Solved

Problem with Dynamically created Button and Assosiated Sprite

Posted on 2008-06-12
5
633 Views
Last Modified: 2010-10-05
In AS3, I want to create a function that will create multiple instances of 2 linked library items, call it "theAni" and "theButtonHit". theButtonHit is a button clip with a singel graphic fo rthe hit frame only.

Currently I do not have the hit inside the animated button mc as I have situations where there are different or no other mc's and just the button although in this case each of the as created hit buttons will be positioned over an instance of "theAni"

theAni is an animated mc that needs get a gotoAndPlay("play") action.

My initial was to use a function call to make iseveral instances of the btnAni and hit pair and have been somewhat successful but am having issues with targeting. I am not successful in getting the theAni to do anything although it does get displayed on the stage.

Also, I tried creating a container mc within the function as well, adding the hit and ani to it and then adding the container to the display list addChild(myName) to no avail. The instances displayed but was unsuccessful targeting them - specifically theAni mc.

There is probably a much better way to do this and I am sure I could forge a work-around but this is where I am at the moment. It's probably somethig obvious im overlooking and input on the method I using to accomplish the task is apprecatied.






var btnWidth = 109;

var btnHeight = 22;
 

// button names passed to the function

// i get errors without declaring them
 

// these names declared next are actually something like btnProductName, btnDescription etc.

var btnHome, btnA,btnB, btnC, btnD, btnE, btnF;

var hit_button_home, menuBtnA, menuBtnB, menuBtnC, menuBtnD, menuBtnEmenuBtnF;
 

var btnInitX = 64;

var btnInitY = 30;

var btnDX = 111.5; // x position multiplier
 

function makeHeaderBtn(btnNum, hitName, btnName, theMainLabel, altLable1, altLabel2):void {
 
 

	var btnHolder:MovieClip = new MovieClip();

	

	var btnName:Button_Main = new Button_Main(); // animatable MC linked from lib 

	btnName.x = btnInitX + (btnDX *btnNum);

	btnName.y = btnInitY;

	btnName.myLabel = theMainLabel;

	btnName.myAltLabel1 = altLable1;

	btnName.myAltLabel2 = altLabel2;

	

	

	// ADD BUTTON HIT

	var hitName:hitbutton = new hitbutton(); // this is actually a button linked from lib

	hitName.width = btnWidth;

	hitName.height = btnHeight+10;

	hitName.x = btnInitY + (btnDX * btnNum);

	hitName.y = 15;

	hitName.addEventListener(MouseEvent.MOUSE_OVER,doMouseOver);

	

	

	//addChild(btnHolder);

	addChild(btnName);

	addChild(hitName);
 

}
 
 

function doMouseOver(e:MouseEvent):void{

	trace("Activate Home button... " + e.target);

	btnHome.gotoAndStop("stop");

}
 
 

makeHeaderBtn(0, hit_button_home, btnHome, "HOME", "", "");

makeHeaderBtn(1, menuBtnA, btnHome, "Item A", "", "");

makeHeaderBtn(2, menuBtnB, btnHome, "", "Hello", "World"); // etc.

Open in new window

0
Comment
Question by:MartiniMon
  • 2
  • 2
5 Comments
 
LVL 12

Accepted Solution

by:
williamcampbell earned 500 total points
ID: 21773359
Martin,

  I'm Not completely sure what you are trying to do but have created a class for you which will do what you say above...

 I think :)

 Take a look at the code you should be able to learn a few things from it and adjust  it to your needs

WC
HeaderButton.as 

package

{

	class HeaderButton extends MovieClip

	{

		const btnWidth = 109;

		const btnHeight = 22;		

		

		static var btnInitX;

		static var btnInitY;

		static var btnDX; // x position multiplier

				

		public var m_home_button;

		public var m_home_hit_button;

		

		public function HeaderButton ( btnNum, theMainLabel, altLable1, altLabel2 )

		{

				var btnName:Button_Main = new Button_Main(); // animatable MC linked from lib 

				btnName.x = btnInitX + (btnDX *btnNum);

				btnName.y = btnInitY;

				btnName.myLabel = theMainLabel;

				btnName.myAltLabel1 = altLable1;

				btnName.myAltLabel2 = altLabel2;

				

				

				// ADD BUTTON HIT

				var hitName:hitbutton = new hitbutton(); // this is actually a button linked from lib

				hitName.width = btnWidth;

				hitName.height = btnHeight+10;

				hitName.x = btnInitY + (btnDX * btnNum);

				hitName.y = 15;

				hitName.addEventListener(MouseEvent.MOUSE_OVER,doMouseOver);

				

				addChild(btnName);

				addChild(hitName);

		}

		

		private function doMouseOver(e:MouseEvent):void

		{

			trace("Activate Home button... " + e.target);

			m_home_button.gotoAndStop("stop");

		}

	}

}
 

This is how you call it
 

// Stage code
 

// Initialize static variables

HeaderButton::btnInitX = 64;

HeaderButton::btnInitY = 30;

HeaderButton::btnDX = 111.5; // x position multiplier
 

// Build the Menu

var l_menu_buttons:Object; // an array of HeaderButton Classes

l_menu_buttons = new Object ();
 

// Create the Home Button

l_menu_buttons[0] = new HeaderButton ( 0, "HOME", "", "" ); 

// create the Menu Buttons

l_menu_buttons[1] = new HeaderButton ( 1, "Item A", "", "");

l_menu_buttons[1].m_home_button = l_header_buttons[0];

l_menu_buttons[2] = new HeaderButton ( 2, "", "Hello", "World")

l_menu_buttons[2].m_home_button = l_header_buttons[0];
 

// Add all the buttons to the parent (stage?)

for each ( var button in l_menu_buttons )

{

	addChild ( button );

}

Open in new window

0
 
LVL 1

Author Comment

by:MartiniMon
ID: 21773917
Hi WC -

Thanks for the fast response -

So as I understand it, the code within and including the package declaration goes into a separate as files named HeaderButton.as. This is separated from the calling statements which are inserted ina frame.

I tried it both ways and am getting some errors.

If I put the pacage code in a separate file, this error appeared initially:

1017: The definition of base class MovieClip was not found.

If I throw this in at the top of HeaderButton.as
import flash.display.MovieClip;

The error becomes:
1083: Syntax error: package is unexpected.


If I put it all in a frame,
1083: Syntax error: package is unexpected.

Without the import and all the AS on one frame I get this:
1037: Packages cannot be nested.

What am I doing wrong here?

Thanks again!!

//HeaderButton.as 

//import flash.display.MovieClip;
 

package

{

	class HeaderButton extends MovieClip

	{

		const btnWidth = 109;

		const btnHeight = 22;		

		

		static var btnInitX;

		static var btnInitY;

		static var btnDX; // x position multiplier

				

		public var m_home_button;

		public var m_home_hit_button;

		

		public function HeaderButton ( btnNum, theMainLabel, altLable1, altLabel2 )

		{

				var btnName:Button_Main = new Button_Main(); // animatable MC linked from lib 

				btnName.x = btnInitX + (btnDX *btnNum);

				btnName.y = btnInitY;

				btnName.myLabel = theMainLabel;

				btnName.myAltLabel1 = altLable1;

				btnName.myAltLabel2 = altLabel2;

				

				

				// ADD BUTTON HIT

				var hitName:hitbutton = new hitbutton(); // this is actually a button linked from lib

				hitName.width = btnWidth;

				hitName.height = btnHeight+10;

				hitName.x = btnInitY + (btnDX * btnNum);

				hitName.y = 15;

				hitName.addEventListener(MouseEvent.MOUSE_OVER,doMouseOver);

				

				addChild(btnName);

				addChild(hitName);

		}

		

		private function doMouseOver(e:MouseEvent):void

		{

			trace("Activate Home button... " + e.target);

			m_home_button.gotoAndStop("stop");

		}

	}

}

// This is how you call it

 

// Stage code

 

// Initialize static variables

HeaderButton::btnInitX = 64;

HeaderButton::btnInitY = 30;

HeaderButton::btnDX = 111.5; // x position multiplier

 

// Build the Menu

var l_menu_buttons:Object; // an array of HeaderButton Classes

l_menu_buttons = new Object ();

 

// Create the Home Button

l_menu_buttons[0] = new HeaderButton ( 0, "HOME", "", "" ); 

// create the Menu Buttons

l_menu_buttons[1] = new HeaderButton ( 1, "Item A", "", "");

l_menu_buttons[1].m_home_button = l_header_buttons[0];

l_menu_buttons[2] = new HeaderButton ( 2, "", "Hello", "World")

l_menu_buttons[2].m_home_button = l_header_buttons[0];

 

// Add all the buttons to the parent (stage?)

for each ( var button in l_menu_buttons )

{

	addChild ( button );

} 

Open in new window

0
 
LVL 1

Author Comment

by:MartiniMon
ID: 21773926
BTW the parent is an empty MC instance on the stage at root level.

0
 
LVL 12

Assisted Solution

by:williamcampbell
williamcampbell earned 500 total points
ID: 21774228
Yes you got it right
Put the import after package ... I didnt actually compile the code :)

package
{
       import flash.display.MovieClip;
       rest.....

0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

This is intended to introduce all collision detection principles in flash, their strengths, weaknesses and workarounds. The main method for Collision Detection in flash is using hitTestObject. But unless you'll be pushing rectangular shapes without …
I have been doing hardcore actionscripting for some time; and needless to say I have faced a lot of problems in just understanding others' code rather than understanding what the code executes. A programmer's life can become hell when there are a lo…
The goal of the tutorial is to teach the user how to how to load their YouTube profile onto Flash Media Live Encoder.
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now