Dispatching Events from Flash MovieClip Timeline to Actionscript 3 Class Code
Being a hardcore Class-based OOP programmer, I almost always tell people not to put code in the flash professional timeline, but rather to put it in an actionscript 3 class. However, there are always exceptions, and this is one of them. Suppose you have an animation in Flash professional where an animation plays for X number of frames. Now suppose you want a function in one of the classes in your main code to be triggered when the timeline has finished playing. To make it even more difficult, the designers might want the function to be triggered at some number of frames after the animation has started, not necessarily once it’s finished.
Luckily, this is pretty easy to do. Just put some code to dispatch an event in your movie clip timeline on the frame corresponding to when you want the event to be dispatched. dispatchEvent(new Event(“CUSTOM_MC_EVENT”, true); Then back in your actionscript class you just add an event listener to an instance of that movie clip.
customMC = new CustomMC; _customMC.addEventListener(“CUSTOM_MC_EVENT”, onCustomMcEvent); private function onCustomMcEvent(event:Event):void { trace(“Received the message from custom mc!”); } |