How to bind event for MovieClip on the next frame?
I have flash project (ActionScript 3), which has a several frames. Each frame contains some unique controls (buttons, textInputs, etc) with unique ids. I want to add event listeners to these controls:
this.gotoAndStop(2);
trace(AddResource); //null !!!
AddResource.addEventListener(MouseEvent.C开发者_StackOverflow社区LICK,AddRes); // Error
I don't know, how to define time, when AddResource will be loaded, after gotoAndStop. I tried to use setTimeout - it works sometimes and makes unwanted delays. So it is bad idea, in my opinion.
Each frame has a lot of controls, they might be changed in future - so, I don't want to use this way (Get MovieClip in another frame).
If it is possible, I want to keep ActionScript code in *.as files, not on timeline (flash cs5: when i change frames in a movieclip, event listeners pointed to one object in the previous frame are removed).
How to solve this problem?
If I read this right, I see two options.
Create an ENTER_FRAME listener for the MovieClip with all the stuff in it. Track what frame number you are on, when it changes, then you can add your new listeners. This may end up with a memory leak though when the old listeners are no longer in play.
Let your events bubble up to the parent MovieClip, who then catches them, figures out who actually dispatched the event, and then do the proper thing.
To be honest, though, I would use a Sprite to hold all of the stuff for each frame (ie, one Sprite per frame), and then change visibility to simulate playhead movement.
精彩评论