Main Content
Duplicating Movie Clips
Welcome to Foundation Flash's tutorials once again! This tutorial is about duplication - useful for virtually any game - enemies, powerup and such like can all be duplicated. This is what we will be making:
The function we will use is the function: duplicateMovieClip()
To start off, make a Movie Clip that will hold most of our ActionScript. In my movie above, it is the "lots" text. Select it, and then press F9 to open up the actions panel. Type:
onClipEvent(load){ var int1 = 2 }
What we have just done is create a new variable called "int1" and make the value of it equal two. This variable will be used to make the instance names of our duplicated Movie Clips unique. You will see how to do that soon. First, create the Movie Clip that you want to duplicate and call it anything. Make the instance name 'object1', and in its action panel, type:
onClipEvent(load){ this._x = Math.random()*300; this._y = Math.random()*300; }
What we have just told Flash is when the Movie Clip loads, or is duplicated, then it will appear in a random position on the stage, assuming your stage size is 300 X 300. Now go back to the Movie Clip holding your "int1" Movie Clip. Add:
onClipEvent(enterFrame){ int1++ }
So, every time you enter the frame, the int1 variable will increase by 1. In case you did know "++" means add one. Thankfully we only have one line left. So, in between onClipEvent(){ and int1++, type:
duplicateMovieClip(this._parent.object1,"object" + int1,(1000+int1)) ;
So, we have used the duplicateMovieClip() function to duplicate our "object1" Movie Clip. The structure of this function is: duplicateMovieClip(object you want to duplicate, name of new Movie Clip, depth of new Movie Clip) So we have duplicated object1, every time we enter the frame, and each new instance will have an instance name of "object" and then whatever number the variable "int1" is at the time (which is always increasing). Then we have made its depth unique. A Movie Clip's depth, by the way, is it's depth on the stage. So, if it overlaps with something on the stage, then if it has a higher depth than the object, it will be displayed above it. Each has to be different, hence the int1 added to 1000. The high number of 1000 basically ensures that they will be on top of everything else - although 2384 would do just as well. Press ctrl/command + enter to test your movie and see it working (if you've done it right). Well, I am afraid that that brings us to the close of this tutorial, thank you for reading, and I hope you will join us for our next tutorial,
Harry.