Determining When A Movie Clip Is Initialized


Strict Standards: call_user_func_array() expects parameter 1 to be a valid callback, non-static method CodeColorerLoader::CallBeforeHighlightCodeBlock() should not be called statically in /home/iloveitaly/mabblog.com/blog/wp-includes/plugin.php on line 170

Strict Standards: Non-static method CodeColorer::GetInstance() should not be called statically in /home/iloveitaly/mabblog.com/blog/wp-content/plugins/codecolorer/codecolorer.php on line 218

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/iloveitaly/mabblog.com/blog/wp-content/plugins/codecolorer/codecolorer-core.php on line 53

Deprecated: preg_replace(): The /e modifier is deprecated, use preg_replace_callback instead in /home/iloveitaly/mabblog.com/blog/wp-content/plugins/codecolorer/codecolorer-core.php on line 54

Warning: shuffle() expects parameter 1 to be array, null given in /home/iloveitaly/mabblog.com/blog/wp-content/plugins/wordpress-support/wordpress-support.php(10) : runtime-created function(1) : eval()'d code(1) : eval()'d code on line 1

Strict Standards: call_user_func_array() expects parameter 1 to be a valid callback, non-static method CodeColorerLoader::CallAfterHighlightCodeBlock() should not be called statically in /home/iloveitaly/mabblog.com/blog/wp-includes/plugin.php on line 170

Strict Standards: Non-static method CodeColorer::GetInstance() should not be called statically in /home/iloveitaly/mabblog.com/blog/wp-content/plugins/codecolorer/codecolorer.php on line 226

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());
box.play();

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;
trace(this.textHolder.textField._height);

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:

dispatchEvent({type:"inited"});

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

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

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 this.play() 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.

0 Response to “Determining When A Movie Clip Is Initialized”


  • No Comments

Leave a Reply





Strict Standards: call_user_func_array() expects parameter 1 to be a valid callback, non-static method K2::init_scripts() should not be called statically in /home/iloveitaly/mabblog.com/blog/wp-includes/plugin.php on line 405