Animating Dynamic MovieClips - Page 8
       by kirupa  |  30 August 2006

We wrapped up the main part of the tutorial on the previous page. In this, the last page, we tie up some unfinished ends!


Putting it All Together
At this point, you re-created the mouse-trail like effect as well as learned about how each line of code works. In this section, let's go back and re-visit some of the important concepts beyond just the code that I hope this tutorial communicated.

Obviously, learning how to use attachMovie is a big part of this tutorial. Creating re-usable movie clip is both a great way to reduce file size, but it also makes maintenance and future updates much easier when all you have to do is change one movie clip instead of several individual movie clips.

Using attachMovie has its own set of issues that need to be addressed. One major issue is being able to assign unique values to the the newly attached movie clip's properties and variables. There is no easy way to use an onLoad event handler with a dynamically generated movie clip placed on stage via attachMovie, loadMovie, createEmptyMovieClip, etc. So, in this tutorial, I explained how to pass in the values for the properties of your movie clip object as part of the attachMovie function's argument itself. There are other, more indirect ways, of simulating onLoad, and the following code provides one way of using the onEnterFrame event handler and deleting it after it has gone through one iteration:

var newMC:MovieClip = this.attachMovie("blueCircle", "blue"+count, 10000+count);
newMC.onEnterFrame = function() {
this._x = xPos;
this._y = yPos;
this._alpha = 10+Math.random()*40;
this._xscale = this._yscale=scale;
delete this.onEnterFrame;
};

The above method approach has its drawbacks also. For example, it is tricky to both assign your initial properties using onEnterFrame, deleting onEnterFrame, and then creating another onEnterFrame event handler to deal with the animation. Until, hopefully, future versions of Flash address this issue, you can read senocular's FAQ on this as well as take a look at some solutions at the always-useful http://proto.layer51.com/ site.

Finally, keeping each movie clip's properties and variables from interfering requires some planning. If a variable happened to accidentally be declared outside the scope of an individual movie clip, then you will find that this particular variable's data is shared across all movie clips. That might be useful in some cases, but when each movie clip is part of an animation with its own scale and alpha values, a global variable is not preferable.


Phew! With that, I hope this tutorial helped give you a better understanding of using attachMovie and dealing with some of the issues that you can encounter with it. I have provided the source file for my version of the example animation you saw on the first page.

Just a final word before we wrap up. If you have a question and/or want to be part of a friendly, collaborative community of over 220k other developers like yourself, post on the forums for a quick response!

Kirupa's signature!

 

1 | 2 | 3 | 4 | 5 | 6 | 7 | 8




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.