Hello All, Ok im finally teaching myself AS3 ! Im doing Lynda.com and bought the book “Learning ActionScript 3.0”. Ive been at this all day and trying to get a grasp on this mc animated btn thing. My code is below:
function btn1Over(event:MouseEvent):void {
btn1.gotoAndPlay("over");
}
function btn1Out(event:MouseEvent):void {
btn1.gotoAndPlay("out");
}
function btn1Click(event:MouseEvent):void {
btn1.gotoAndPlay("click");
gotoAndPlay("myFrameLabel");
}
btn1.addEventListener(MouseEvent.ROLL_OVER, btn1Over);
btn1.addEventListener(MouseEvent.ROLL_OUT, btn1Out);
btn1.addEventListener(MouseEvent.CLICK, btn1Click);
My question is…if i put button “btn1” inside a movie clip on the stage…(ex..”btn1” would be inside movieclip titled “topLeft_mc”) and have a second movie clip on the stage titled “middle_mc” with a frame label titled “startanimation”....what would i need to put in order for “btn1” to comminicate with “middle_mc” to play “startanimation”? Sheww! LOL !
Also what would I put in order to have the “btn1” communicate with a frame label” on the main timeline (root)?
Thanks for the help guys! Cheers, Jason
When communicating between objects in different hierarchies I almost always use event dispatchers and event handlers.
For example:
in the actionscript of your movieclip middle_mc add the following:
stage.addEventListener("ANIMATE_MIDDLE", startAnimation);
function startAnimation(e:Event):void
{
gotoAndPlay("startanimation")
}
then in the actionscript inside your btn1Click() mouse click handler add:
stage.dispatchEvent(new Event("ANIMATE_MIDDLE"))
That would probably be a functional start, but you should take the time to learn all about event dispatching and handling. Very useful. Especially custom events
(Events are also available in AS2 but never really seem to get get used much by AS2 developers for some reason)
You could acheive the desired result by coding paths through the hierarchies e.g. in btn1Click():
parent.parent.middle_mc.gotoAndPlay("startanimation")
(assuming the hierarchy you described of root.top_left.btn1 and root.middle_mc )
But then if you make middle_mc the child of another movieclip on the root the code would break. Using event dispatching and handling it would continue to work no matter how you changed the hierarchy.
Ah I see…interesting…Thank you for the help…so would this be correct?
function btn1Click(stage.dispatchEvent(new Event("ANIMATE_MIDDLE"):void {
btn1.gotoAndPlay("click");
parent.parent.middle_mc.gotoAndPlay("startAnimation");
}
then on frame 1 of middle_mc I would put…
stage.addEventListener("ANIMATE_MIDDLE", startAnimation);
function startAnimation(e:Event):void {
gotoAndPlay("startAnimation")
}
and have a frame label on the timeline of “middle_mc” titled “startAnimation”?
Thanks a ton! Cheers!
my_design09 said
Ah I see…interesting…Thank you for the help…so would this be correct?function btn1Click(stage.dispatchEvent(new Event("ANIMATE_MIDDLE"):void { btn1.gotoAndPlay("click"); parent.parent.middle_mc.gotoAndPlay("startAnimation"); }then on frame 1 of middle_mc I would put…
stage.addEventListener("ANIMATE_MIDDLE", startAnimation); function startAnimation(e:Event):void { gotoAndPlay("startAnimation") }and have a frame label on the timeline of “middle_mc” titled “startAnimation”?
Thanks a ton! Cheers!
No.. just add the line I posted inside the btn1Click handler, not in its argument list:
function btn1Click(event:MouseEvent):void
{
stage.dispatchEvent(new Event("ANIMATE_MIDDLE"));
btn1.gotoAndPlay("click");
parent.parent.middle_mc.gotoAndPlay("startAnimation");
}
The other part (in middle_mc) should be okay.
Whoops .. cut and paste error .. don’t include the ‘parent.parent … ” line

