Skip to content

Determining When A Movie Clip Is Initialized

Categories: Flash

Table of Contents

This is a common problem in Flash, determining when a MC is fully initialized. This problem only really arises when you are using attachMovie() to dynamically make/attach MC’s to the stage. An example of this problem is if you have a MC with timeline animation that you are going to attach to the stage. The timeline animation has stop() on its first frame and its last frame. You use this code to attach it to the stage:

this.attachMovie("boxClip", "box", this.getNextHighestDepth());;

The attached “box” MC wont play (Here’s an example of what I’m explaining). Other common problems like this are trying to grab the height of a dynamic textfield with autoSize = true contained in a MC that you have just attached to the stage and assigned alot of text to the internal textfield. Heres an example:

this.attachMovie("textContainer", "textHolder", this.getNextHighestDepth());
this.textHolder.textField.text = lotsOfText;

The textfield will not trace its height after the text has been inserted and the textfield has auto-sized, it was report whatever size you originally assigned the textfield (Here’s an example of what I’m explaining).

Your probably wondering how the heck you fix this problem, well, actually its really simple. By using EventDispatcher we can easily solve this problem. I discovered that after a MC has run through the code on its first frame everything in the MC is initialized including all child MC’s. Using this information I added this line of code to the end of the first frame’s actions:


Then for the attach code for the first example with the animated MC would look like this:

this.attachMovie("boxClip", "box", this.getNextHighestDepth());
box.addEventListener("inited", function(){;});

The initBroadcaster() method is from my Object Prototypes and simple initialized the object that the method is applied on as a EventDispatcher. If you dont know anything about EventDispatcher or listeners, I suggest you learn about them; they are one of the most helpful things in AS, this tutorial should give you what you need to know about EventDispatcher if you haven’t used it already. So after initializing the box MC as a EventDispatcher I added a function listener to the “inited” event that will be broadcasted by the box once it is fully initialized. The function is executed in the scope of the box, because of this the ‘this’ variable equals the box MC, thus will cause the box MC to start playing. If you didn’t want that function to exact in the boxes scope you can use the Delegate class to execute a function is different scope (Flash 7.2 also comes with a Delegate class but Steve’s Delegate class that I linked to is better). If you want to learn more about Delegates and how to use them read this tutorial. You can get both the updated animation initialization and the textfield initialization files here.