Both Flash movies above use the same element: one of those familiar radiator Movie Clips we've seen in many other projects. But note the obvious difference: in the lefthand movie the Movie Clip occurs only once. When it completes its cycle it shifts randomly to a new position on the screen. Something altogether more interesting is going on in the lefthand movie.
Though there is only one Movie Clip in the left movie (at the start, anyway), that clip is scripted to duplicate itself at a random interval. Even more intriguing, the copied clip brings with it the script attached to its parent; it too makes a copy of itself, this copy makes a copy, and so on until a specified limit is reached. Because each original clip relocates itself after copying, we seem to see a random pattern of radiating ripples or raindrops.
Scripting
Here's the script that's responsible for all this fun; as noted above, it's attached to the single Movie Clip in the movie, which sits in the solitary frame of Layer 1.
I've included some line spacing to separate various parts of the code.
onClipEvent(enterFrame){
if(getTimer() > _root.rainDelay){
_root.n++;
if(_root.n>8) _root.n = 0;
duplicateMovieClip(this, "drop", _root.n);
this._x = Math.round(Math.random()*400);
this._y = Math.round(Math.random()*400);
_root.rainDelay = getTimer()+Math.ceil(Math.random()*1000)+500;
}
}
First note that nothing happens unless the current clock value exceeds a value stored in a variable called rainDelay. This variable is initialized for the first run in a load handler. The delay keeps the time between drops reasonable.
The next two lines refer to a global variable called _root.n. This arbitrarily named variable is used to assign the copies of the droplet movie to different levels within their layer. (See our coverage of hierarchy a few weeks back.) If we don't do this, the copies will end up in the same layer, meaning that the new one will annihilate the old one, because though a level may contain many different Movie Clips, it can only contain one instance of each of them.
We test the value of _root.n against a maximum value of 8, dropping it back to 0 when it exceeds that value. This means that each Movie Clip can make only 8 copies of itself before it starts erasing the old copies. If we don't set this limit, the screen will fill up with copied clips and eventually the Flash player and/or browser will crash.
The next line, set off all by itself, contains the major magic of this script. The duplicateMovieClip() method requires three arguments: the name of the clip being duplicated, the arbitrary name that will be given to the new copy, and a stacking level.
Note that all copies of the droplet clip will be called "drop." This may seem strange but it's legal enough, and since we never need to refer to the copies in some more particular way, it works just fine.
The next two lines move the original Movie Clip to a randomly chosen location on the screen, whereupon both it and its copy repeat the instructions in this script.
The last line of the script should be familiar to you from other demonstrations involving getTimer(). Here we record a new delay value for the copying process.
Source file
The source file for the raindrop duplication demo is called rainDropsDupe3.fla. It can be found in MMShare/duplication/ on Crow.
