AS: Using Duplicated Movie Clips
One of my favourite Flash features is the ability to duplicate Movie Clips. This has a wide range of uses, from creating enemies in a game to making a snazzy mousetrail.
Basically, there are two ways to do this: attachMovie and duplicateMovieClip.
They vary only in the way the MC is duplicated; once the dupe MCs are created, they can be treated in the same way.
--------------------------------------------------------
duplicateMovieClip
This code assumes that an MC with the Instance Name 'orig' exists on the Stage, and the duplicating is going to occur as a result of pressing a button.
In its simplest form, duplicateMovieClip looks like this:
on(press){
duplicateMovieClip(_root.orig, "MC01", 1000);
}
The above code will create a new MC with the Instance Name "MC01" in the same position as the original MC (_root.orig), at a depth of 1000.
Quick explanation of this code:
The first parameter (_root.orig) is the MC to be duplicated
The second parameter ("MC01") is the Instance Name of the new MC
The third parameter (1000) is the depth at which the new MC will be created. If you create a MC at a depth that's already occupied by an MC, the older MC will be 'overwritten'
Generally when dealing with duplicated MCs, you'll be creating more than one. The code gets a little more complicated, since you'll need to use a for loop
on(press){
for(i=1;i<100;i++){
duplicateMovieClip(_root.orig, "MC"+i, 1000+i);
}
}
This will create 99 MCs, with the Instance Names "MC1" to "MC99", at depths 1001 to 1099.
Unfortunately, you won't be able to see them, since they'll be stacked on top of one another. So, we'll now adjust some properties as we create them:
on(press){
for(i=1;i<100;i++){
duplicateMovieClip(_root.orig, "MC"+i, 1000+i);
_root["MC"+i]._x=random(550);
_root["MC"+i]._y=random(400);
_root["MC"+i]._alpha=random(100);
_root["MC"+i]._rotation=random(360);
_root["MC"+i]._width=random(40)+20;
_root["MC"+i]._height=random(40)+20;
}
}
And if you add this code to your _root.orig MC, fun can be had by all. Honest.
onClipEvent(enterFrame){_rotation+=5;}
--------------------------------------------------------
Code sample: Simple Mousetrail
Add this to your _root.orig MC. No button necessary.
onClipEvent(load){i=0;}
onClipEvent (mouseMove){
i++;
this.duplicateMovieClip("MC"+i, i+10);
_root["MC"+i]._x = _root._xmouse;
_root["MC"+i]._y = _root._ymouse;
}
onClipEvent (enterFrame) {
if (_name=="orig") {
this._visible=0;
}else{
_alpha-=2;
_xscale-=2;
_yscale-=2;
if(_alpha<=2) {removeMovieClip(this);}
}
}
--------------------------------------------------------
Code sample: Twinkle
Use a dark background for your .fla
Change your _root.orig MC to a small white dot, and add actions:
onClipEvent(load){G=random(500);_alpha=0;}
onClipEvent (enterFrame) {
G++;
if(G>500){_alpha+=5;}else{_alpha-=5;}
if(_alpha>99){G=random(400);}
}
Add these actions to a frame on which _root.orig exists:
for(i=1;i<250;i++){
duplicateMovieClip(_root.orig,"MC"+i,1000+i);
_root["MC"+i]._x=random(Stage.width);
_root["MC"+i]._y=random(Stage.height);
}
--------------------------------------------------------
And now onto Method 2. I don't use this very often, since I like to be able to add code to the original MC. However, this is in some ways 'cleaner' than duplicateMovieClip, since it duplicates from the Library, so you don't need an Instance of your MC on stage. It's also a better method if you work with Classes/OOP (neither of which are my strong points =).
attachMovie
First of all, right-click your MC in the Library, and select 'Linkage'
Tick the 'Export for Actionscript' box, and give it an identifier... 'orig'
The procedure for using attachMovie is very similar to duplicateMovieClip. The only difference is that you use the Linkage Name, rather than the Instance Name, for the first parameter.
Add this to your button:
on(press){
for(i=1;i<99;i++){
_root.attachMovie("orig","MC"+i, 1000+i);
_root["MC"+i]._x=random(550);
_root["MC"+i]._y=random(400);
_root["MC"+i]._rotation=random(360);
var col=0xFFFFFF;colswap=random(8);
if(colswap==0){col=0xFFFFFF;}
if(colswap==1){col=0xE6E600;}
if(colswap==2){col=0x595959;}
if(colswap==3){col=0x8A00E6;}
if(colswap==4){col=0x00E600;}
if(colswap==5){col=0xE68A2E;}
if(colswap==6){col=0x0000E6;}
if(colswap==7){col=0xE60000;}
new Color(_root["MC"+i]).setRGB(col);
}
}
--------------------------------------------------------
More reading on duplicated/attached MCs:
http://livedocs.macromedia.com...MX_2004&file=00001496.html
http://www.macromedia.com/supp...ctionscript_dictionary194.html
http://www.actionscripts.org/tutorials/beginner/attachMovie/index.shtml
http://actionscript-toolbox.com/sampleattachmovie.php
http://livedocs.macromedia.com...MX_2004&file=00001487.html
http://www.hed.swin.edu.au/design/tutorials/flash/attachmovie/index.php
=============================
AS: Sections
AS: Preloader
AS: Sound
AS: Basic Movement
AS: Collision Detection by BleeBlap
AS: Random Movement by Begoner
AS: Movement on slopes by Joelasticot
AS: Arrays
AS: Save and Load
AS: Clock by Glaiel_Gamer
AS: Pong physics & gravity by Dark_Toaster
AS: API by -liam-