Flash AS3 - Events from Child to Parent Timelines
What it is I would like to do seems simple enough, but is confounded by the fact that I want to work within Flash's timelines, as opposed to within custom classes, .as files, flex, etc. I am an artist, and code is only as reusable as my assets (created/animated in Flash), so I tend to work in the timeline.
I want to have a script at the end of a child's timeline progress the timeline of its parent. Something like this would be on the end frame of the child, if it were a real AS3 script: this.parent.gotoAndPlay(2);
I have done "fixes" that work, but slow my program to an unacceptable rate. For example: I will save a public static Boolean in an imported custom class and have a "listener" on parent frame 1:
import customClass; if (Boolean = true) {gotoAndPlay(3);} // Waiting for child
on parent frame 2:
gotoAndPlay(1); // This creates the cheap loop
and on the last child frame:
import customClass; Boolean = true // Tells the parent when to leave its loop
This works, but is obviously not the best way... What I need is recursion within the same timeline frame, but without a stack overflow or numbered increments.
I suspect the answer lies with EventListeners, but I don't want to trigg开发者_如何转开发er it with a MouseClick or a Timer, I want to trigger it like this: new EventListener:EventListener (ThatGlobalVarAtTheEndOfYourChildTimelineHasBeenChanged); Then it'll progress when the Boolean has been changed to true.
I'm sure this is easy, and I feel like an idiot asking something so simple, but all of the documentation has me working on the stage, or with an object within an object I create in code, but it's not that easy with what I want to do because I'm making complex animations that use several timelines. Flash knows which object is within what, not me or my classes.
Thank you anyone for your help.
The main timeline could dispatch and listen to events and send the necessary info to the relevant child.
in a child instance, you could write.
var dispatcher:MovieClip = this.root as MovieClip; dispatcher.dispatchEvent( new Event('last frame reached') );
and in the main timeline
this.addEventListener( "mc1 last frame reached" , eventListener ); function eventListener(event:Event):void { //should return your event name trace( event.type ); //you could use a switch statement to redirect to a specific function switch(event.type ) { case "mc1 last frame reached": this.instanceName1.instanceName2.gotoAndPlay(n); break; case "mc2 last frame reached": this.instanceName1.instanceName4.gotoAndPlay(n); break; ///etc... } }
I would personally use a custom event, where i could pass the instance name of the child and whatever properties i would need in the main timeline to trigger a new function. The child could dispatch the event, but I favor using the root as a front controller.
精彩评论