Over the past couple years the majority of my development focus has been centered around Flex, and having recently started going back to developing in Flash, I’ve come to realize how incredibly SPOILED Flex has made me!
Example: In Flex, when developing components that you want to have multiple views based on user input, or any other laundry list of conditions, you have STATES. In Flash, you have MovieClips with multiple frames, or the option to write all of your logic and views using Actionscript (a huge pain in the ass!). To give you a simple example, I have a login menu that needs to have 2 views: one for the basic login, and a second for displaying a captcha image and additional login inputs. Sounds simple enough, I just make a new MovieClip, put my basic login options on frame 1 and give it a label, then put my captcha options on a different fram and give it a label. So far so good. Now I create the backing class that is going to provide the logic for my login menu. I start to write the class out and add a simple event listener to my submit button.
Enter problem #1: Objects within a MovieClip are not immediately available to the logic class. You have to listen for an Event.ADDED event then add any listeners or perform any other interaction with the objects in your MovieClip. Ok, simple solution, add the listener in your constructor, then call an init() method to add the listeners.
Enter problem #2: If you want to show the captcha frame (or interact with any elements on a frame other than frame 1), you will get tons of null object reference, and other errors. Basically, you can’t interact with any objects off of frame one since you don’t know when they will be loaded.
Enter the SOLUTION: I created a class called MultiFrameMovieClip based off of MovieClip that adds functionality to ensure that all frames are loaded prior to any initialization taking place. To use this class, simply extend MultiFrameMovieClip instead of MovieClip for any MC’s that you need to have access to objects on multiple frame. In your logic class, override protected function init() and add any initialization (i.e. any code that requires you have access to the objects in your MC) in this method (or subsequent methods called after this method).
* The MultiFrameMovieClip adds logic to ensure that all frames
* of the MovieClip are loaded prior to any interaction with the
* child objects.
* More info:
* @author Kyle Tyacke
public class MultiFrameMovieClip extends MovieClip
public function MultiFrameMovieClip()
// Jump to the last frame to ensure that all
// frames are loaded
this.gotoAndStop( this.totalFrames );
this.addEventListener( Event.ENTER_FRAME, handleEnterFrame );
private function handleEnterFrame( evt:Event ):void
this.removeEventListener( Event.ENTER_FRAME, handleEnterFrame );
this.addEventListener( Event.RENDER, handleRender );
private function handleRender( evt:Event ):void
this.removeEventListener( Event.RENDER, handleRender );
* Override this method in the child class. When this method
* is called you will have access to all children across all
* frames of your MovieClip.
protected function init():void
trace("Init function must be overridden by child class.");
You can download the class file here: MultiFrameMovieClip.as