00:00
00:00
Newgrounds Background Image Theme

hyperheartcom just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

As: Using Duplicated Movie Clips

18,308 Views | 39 Replies
New Topic Respond to this Topic

As: Using Duplicated Movie Clips 2005-06-28 09:25:43


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: Main

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-


- - Flash - Music - Images - -

BBS Signature

Response to As: Using Duplicated Movie Clips 2005-06-28 09:28:55


Thats a very nice code you got Denvish! Unfortunately, it still seems so complicated to me. Right now, I am working on a almost complete GTA engine, and I might have to use this so I better get learning!

Response to As: Using Duplicated Movie Clips 2005-06-28 09:34:53


At 6/28/05 09:28 AM, Dancing-Thunder wrote: Unfortunately, it still seems so complicated to me.

Yeah, it was a bit of a long one - although large chunks of it are just sample codes that you can try out. I'd recommend creating a new .fla to test this stuff out with before trying to incorporate it into your game - all you need to play with is two symbols (an MC and a button), and one frame.


- - Flash - Music - Images - -

BBS Signature

Response to As: Using Duplicated Movie Clips 2005-06-28 09:45:01


I use duplicated movie clips all of the time, but I can't understand that code - too long! :)

Response to As: Using Duplicated Movie Clips 2005-06-28 09:59:34


Hey Denvish next time you jot down the list could you do it in alphabetical order? Not that big of a deal but it'll make it easier to find the links quickly.

Response to As: Using Duplicated Movie Clips 2005-07-13 11:07:09


Nice,it helped me with a fire effect,thanks!


BBS Signature

Response to As: Using Duplicated Movie Clips 2006-01-03 13:02:09


how do i duplicate 2 different movie clips the same number of times?
cos i am making a game with 5 characters, each is duplicated 20 times


BBS Signature

Response to As: Using Duplicated Movie Clips 2006-03-05 21:28:32


I have a question. How do you get a MC to duplicate at random times?

Response to As: Using Duplicated Movie Clips 2006-03-21 21:01:36


is there anyway to have the duplicated movieclip to have a bunch of AS on it?
because i want to randomly dupe my enemies but they need tohave a lot of AS on them

Response to As: Using Duplicated Movie Clips 2006-03-21 22:08:32


At 3/21/06 09:50 PM, pyro111 wrote:
At 3/21/06 09:01 PM, -fenix- wrote: is there anyway to have the duplicated movieclip to have a bunch of AS on it?
because i want to randomly dupe my enemies but they need tohave a lot of AS on them
Put the actionscript on the moveclip that is going to be duped.

me so stupid

k one problem.... when the new dupes appear, all the old ones delete... any way to get around this?

Response to As: Using Duplicated Movie Clips 2006-04-08 03:33:42


i got a problem. im makin one of those games were u buy units. the buttons 2 buy thm are at the top of the screen. only when i click it, it duplicates the movie clip 99 times! anyway to fix this, the script i am using in simple.
on the movie clip "inf" (the unit):
onClipEvent(load){
duped = false;
this.gotoAndPlay("walk");
speed = 2;
}
onClipEvent(enterFrame){
if(!this.hitTest(_root.castle)){
this._x += speed;
}
if(duped){
for(i=1;i<100;i++){
if(_root.population < _root.poptot){
_root.population ++;
duplicateMovieClip(_root.inf, "MC"+i, i);
_root["MC"+i]._x = -56.4;
_root["MC"+i]._y = random(10)+251.7;
duped = false;
}
}
}
}

on the button:
on(release){
_root.inf.duped = true;
}

any help would be greatly appresiated


BBS Signature

Response to As: Using Duplicated Movie Clips 2006-04-08 03:53:06


At 4/8/06 03:33 AM, Hoeloe wrote: i got a problem. im makin one of those games were u buy units. the buttons 2 buy thm are at the top of the screen. only when i click it, it duplicates the movie clip 99 times! anyway to fix this, the script i am using in simple.
on the movie clip "inf" (the unit):

Just remove the for loop in the inf enterFrame actions (for(i=1;i<100;i++){})


- - Flash - Music - Images - -

BBS Signature

Response to As: Using Duplicated Movie Clips 2006-04-08 03:56:33


ok, i fixed my problem, now i got another 1 :P whenever i make a new duplicate, the old one dissapears how do i fix this PLEASE!!!!!


BBS Signature

Response to As: Using Duplicated Movie Clips 2006-04-08 03:58:47


It's because you're creating the new MC at the same depth as the previous one
Try this

_root.population ++;
duplicateMovieClip(_root.inf, "MC"+_root.population, _root.population);
_root["MC"+_root.population]._x = -56.4;
_root["MC"+_root.population]._y = random(10)+251.7;


- - Flash - Music - Images - -

BBS Signature

Response to As: Using Duplicated Movie Clips 2006-04-08 07:21:48


ive got a problem about duplicating..apart from it being hard and very confusing i have succeded until now..i cant get the duplicates name
for (q=1; q<2; q++) {
duplicateMovieClip(_root.rock, "rock2"+q, 1-q);
_root["rock2"+q]._x = 60;
_root["rock2"+q]._y = 160;

if i say wanted that to be in a hittest script would the name be:
_root["rock2"+q]
_root.rock2 +q
_root.rock21
ived tryed all of the above i think..also is there a way to get duplicated names?
getpropertyinstancedname or something?

Response to As: Using Duplicated Movie Clips 2006-04-08 07:28:58


http://www.newground../topic.php?id=378524
not working for me so no point linking me

Response to As: Using Duplicated Movie Clips 2006-04-08 07:40:58


At 4/8/06 07:21 AM, Richy-gorgan wrote: ive got a problem about duplicating..apart from it being hard and very confusing i have succeded until now..i cant get the duplicates name
for (q=1; q<2; q++) {
duplicateMovieClip(_root.rock, "rock2"+q, 1-q);
_root["rock2"+q]._x = 60;
_root["rock2"+q]._y = 160;

if i say wanted that to be in a hittest script would the name be:
_root["rock2"+q]
_root.rock2 +q
_root.rock21
ived tryed all of the above i think..also is there a way to get duplicated names?
getpropertyinstancedname or something?

You can run another FOR loop to check your hitTests, eg:

for (r=1; r<2; r++) {
if(this.hitTest(_root["rock2"+r])){
//do stuff
}
}

(this code would be run on your player MC)

If you want to find out the name of a duped MC, you could run this on the original MC:

onClipEvent(enterFrame){
if(this._name!="rock"){
trace(this._name);
}
}

You might be better off running your hitTests from the rock to the player, rather than the other way round

onClipEvent(enterFrame){
if(this.hitTest(_root.player)){
//do stuff
//_root.player.gotoAndStop("OUCH");
}
}

Whatever code you put on the original MC will also run on the dupes


- - Flash - Music - Images - -

BBS Signature

Response to As: Using Duplicated Movie Clips 2006-04-08 07:44:36


thanks but i got it to work by this tut:
http://www.newground../topic.php?id=378524

i was doing it wrong
sorry for taking up space :0

Response to As: Using Duplicated Movie Clips 2006-04-08 07:55:10


Hi, sorry but i got a new problem, would you believe how many problems dupeing movie clips can cause?
anyway. Im going to make a game with infinite levels, you know, like theres one frame for the levels and lots of variables. and on each level, i want a number of arrows to come out depending on what level you have reached. EG. level one, 1 arrow will come out every 2 seconds, Level 2, 2 arrows per 2 seconds... and so on. Anyway, it doesnt work properly for 3 reasons. in order of priority: 1. i can have 2 coming out and then be unable to change it, or about 16 coming out and be able to change it. 2. the arrows randomly move across the screen, for example it is in the air, thn it is 50 pixels lower. 3. some of the hittests dont work. if you can solve any of these problems please help, it would be greatly apreciated. An example of what i am trying to achieve can be found here


BBS Signature

Response to As: Using Duplicated Movie Clips 2006-04-08 08:40:51


kmon ppl,i need this help :(


BBS Signature

Response to As: Using Duplicated Movie Clips 2006-04-08 08:50:03


New problem, what is the script for a dupe hitting a dupe, cos i want duped arrows hitting duped troops and killing them, plz help, the one i got at the moment doesnt work, it only works for the last arrow loosed:
onClipEvent(enterFrame){
if(this.hit.hitTest(_root["a" +_root.a.i])){
this.gotoAndPlay("die");
_root["a" +_root.a.i].removeMovieClip();
}}


BBS Signature

Response to As: Using Duplicated Movie Clips 2006-05-21 02:21:04


Can someone help me? I need to duplicate multiple movie clips, but i need a new one to come up each time you push a button or whatever so that you keep on making more and more of them.

Response to As: Using Duplicated Movie Clips 2006-05-21 03:23:36


dickhead... dont bump up old TOPICS!!

As: Threads arnt bad to bump up BUT... if you want to ask for help make a new topic!

also... iif you actualy read the tutorial you would know what to do.

Response to As: Using Duplicated Movie Clips 2006-05-30 07:50:43


im just screwing around with some action script.
heres the thing i made
http://img105.images..explosiveshot4oz.swf

when you click somewhere, an explosion happens, but i want it to be like if you try and like rapid fire click all over the place, a bunch of seperate explosions will happen, cuz in its current state, it only moves the movieclip where you click and if its playing it, it jsut keeps playing it. you can kind of tell what im talking about it you play it. can anyone help?

code for the sniper cursor:

onClipEvent(enterFrame){
Mouse.hide();
this.startDrag(true);
}
onClipEvent(mouseDown){
_root.explosion._x = _root.cursor._x
_root.explosion._y = _root.cursor._y
_root.explosion.play();
}

im pretty sure you have to duplicate the explosion MC but i have no clue how to! plz help!


BBS Signature

Response to As: Using Duplicated Movie Clips 2006-06-02 06:33:09


thanks for the 'help'. i guess a lot of people would enjoy working on a collab with YOU. anyway i found a better way than for loops, something your mind is not capable of

Response to As: Using Duplicated Movie Clips 2006-08-02 11:47:32


I'm trying to make a space shooter and the code doesn't work. I try to duplicate a MC called bullet with an instance name of laser. Unfortunatly, it won't work. I want it to be able to shoot. I have every piece of code down BUT the duplicate MC code.

Response to As: Using Duplicated Movie Clips 2006-08-02 23:57:53


Ok... I'm finally getting around to working on the new engine for build a robot but I need some help here...

I'm wanting to organize all my movie clips into a side menu that you can drag parts out of when you want them so they are not all in the middle of the play area.

Unfortunately every time I try to do this nothing happens the way I have planned.

I have the menu named "partmenu"
I have a button in the menu that duplicates a movieclip called "arm1"

the button has this code on it.

on (release) {
_root.partcount + 1;
duplicateMovieClip(_root.arm1, "part"+partcount, 100 + _root.partcount);

//just to randomize placement for testing.
_root["part"+partcount]._x = random(250);
}

and this does make a new clip but removes it when I click the button again... I'm looking for some help getting this menu system ironed out and I'm pulling out my hair. If anyone is willing to help me out I will give you credit in the final game and share some sponsorship money with you if I get it sponsored. (though I might ask for help with other things in the game too...)

I really want to learn this so if you could explain what I'm doing wrong I would appriciate it. also if you could walk me though how to get what I have drawn here to work I would be grateful.

As: Using Duplicated Movie Clips


Visit JackSmack.com and submit your Flash Games!

BBS Signature

Response to As: Using Duplicated Movie Clips 2006-08-17 07:44:16


How do i remove all duped movie clips on the stage? Because i'm making a game with duped enemies that appear after a set amount of time one by one, but if you die i want them to dissappear, is there any way to do this? without using _currentframe?


BBS Signature

Response to As: Using Duplicated Movie Clips 2006-08-17 07:49:27


At 8/2/06 11:57 PM, JackSmack wrote:
I'm wanting to organize all my movie clips into a side menu that you can drag parts out of when you want them so they are not all in the middle of the play area.

the button has this code on it.

on (release) {
_root.partcount + 1;
duplicateMovieClip(_root.arm1, "part"+partcount, 100 + _root.partcount);

//just to randomize placement for testing.
_root["part"+partcount]._x = random(250);
}

I think i know what is wrong

You are setting the variable _root.partcount, but using the variable this.partcount when setting the name. Add _root. and it should work.


BBS Signature

Response to As: Using Duplicated Movie Clips 2006-09-22 18:09:36


I love you, denvish =)


...

BBS Signature