Forum Topic: As: Using Duplicated Movie Clips

(11,992 views • 39 replies)

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
None

Denvish

Reply To Post Reply & Quote

Posted at: 6/28/05 09:25 AM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

Dancing-Thunder

Reply To Post Reply & Quote

Posted at: 6/28/05 09:28 AM

Dancing-Thunder NEUTRAL LEVEL 04

Sign-Up: 04/19/05

Posts: 278

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!


None

Denvish

Reply To Post Reply & Quote

Posted at: 6/28/05 09:34 AM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

Siggy0501

Reply To Post Reply & Quote

Posted at: 6/28/05 09:45 AM

Siggy0501 LIGHT LEVEL 23

Sign-Up: 01/08/05

Posts: 1,563

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


None

GeoffCLogan

Reply To Post Reply & Quote

Posted at: 6/28/05 09:59 AM

GeoffCLogan LIGHT LEVEL 11

Sign-Up: 04/25/04

Posts: 4,423

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.


None

Toast

Reply To Post Reply & Quote

Posted at: 7/13/05 11:07 AM

Toast DARK LEVEL 09

Sign-Up: 04/02/05

Posts: 8,910

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


None

Hoeloe

Reply To Post Reply & Quote

Posted at: 1/3/06 01:02 PM

Hoeloe LIGHT LEVEL 28

Sign-Up: 04/29/04

Posts: 5,005

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

Sex!
------------------------------
Super Nuke Bros. Melee, the web's no. 1 awaited Super Smash Tribute Game!

BBS Signature

None

Rookie209

Reply To Post Reply & Quote

Posted at: 3/5/06 09:28 PM

Rookie209 EVIL LEVEL 06

Sign-Up: 02/20/06

Posts: 32

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


None

fenix

Reply To Post Reply & Quote

Posted at: 3/21/06 09:01 PM

fenix LIGHT LEVEL 22

Sign-Up: 09/07/05

Posts: 1,900

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


None

Pyromaniac

Reply To Post Reply & Quote

Posted at: 3/21/06 09:50 PM

Pyromaniac EVIL LEVEL 18

Sign-Up: 01/14/05

Posts: 2,976

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.


None

fenix

Reply To Post Reply & Quote

Posted at: 3/21/06 10:08 PM

fenix LIGHT LEVEL 22

Sign-Up: 09/07/05

Posts: 1,900

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?


None

Hoeloe

Reply To Post Reply & Quote

Posted at: 4/8/06 03:33 AM

Hoeloe LIGHT LEVEL 28

Sign-Up: 04/29/04

Posts: 5,005

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

Sex!
------------------------------
Super Nuke Bros. Melee, the web's no. 1 awaited Super Smash Tribute Game!

BBS Signature

None

Denvish

Reply To Post Reply & Quote

Posted at: 4/8/06 03:53 AM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

Hoeloe

Reply To Post Reply & Quote

Posted at: 4/8/06 03:56 AM

Hoeloe LIGHT LEVEL 28

Sign-Up: 04/29/04

Posts: 5,005

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!!!!!

Sex!
------------------------------
Super Nuke Bros. Melee, the web's no. 1 awaited Super Smash Tribute Game!

BBS Signature

None

Denvish

Reply To Post Reply & Quote

Posted at: 4/8/06 03:58 AM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

Richy-gorgan

Reply To Post Reply & Quote

Posted at: 4/8/06 07:21 AM

Richy-gorgan EVIL LEVEL 19

Sign-Up: 11/29/05

Posts: 115

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?


None

Richy-gorgan

Reply To Post Reply & Quote

Posted at: 4/8/06 07:28 AM

Richy-gorgan EVIL LEVEL 19

Sign-Up: 11/29/05

Posts: 115

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


None

Denvish

Reply To Post Reply & Quote

Posted at: 4/8/06 07:40 AM

Denvish DARK LEVEL 46

Sign-Up: 04/25/03

Posts: 16,236

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

None

Richy-gorgan

Reply To Post Reply & Quote

Posted at: 4/8/06 07:44 AM

Richy-gorgan EVIL LEVEL 19

Sign-Up: 11/29/05

Posts: 115

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


None

Hoeloe

Reply To Post Reply & Quote

Posted at: 4/8/06 07:55 AM

Hoeloe LIGHT LEVEL 28

Sign-Up: 04/29/04

Posts: 5,005

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

Sex!
------------------------------
Super Nuke Bros. Melee, the web's no. 1 awaited Super Smash Tribute Game!

BBS Signature

None

Hoeloe

Reply To Post Reply & Quote

Posted at: 4/8/06 08:40 AM

Hoeloe LIGHT LEVEL 28

Sign-Up: 04/29/04

Posts: 5,005

kmon ppl,i need this help :(

Sex!
------------------------------
Super Nuke Bros. Melee, the web's no. 1 awaited Super Smash Tribute Game!

BBS Signature

None

Hoeloe

Reply To Post Reply & Quote

Posted at: 4/8/06 08:50 AM

Hoeloe LIGHT LEVEL 28

Sign-Up: 04/29/04

Posts: 5,005

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();
}}

Sex!
------------------------------
Super Nuke Bros. Melee, the web's no. 1 awaited Super Smash Tribute Game!

BBS Signature

None

Cheezie

Reply To Post Reply & Quote

Posted at: 5/21/06 02:21 AM

Cheezie EVIL LEVEL 02

Sign-Up: 03/11/06

Posts: 11

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.


None

shazwoogle

Reply To Post Reply & Quote

Posted at: 5/21/06 03:23 AM

shazwoogle NEUTRAL LEVEL 11

Sign-Up: 09/27/04

Posts: 2,677

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.

None

KLoWnXXX

Reply To Post Reply & Quote

Posted at: 5/30/06 07:50 AM

KLoWnXXX EVIL LEVEL 16

Sign-Up: 02/12/05

Posts: 558

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!

Let's All Go Jump In A Fire Pit! ++The Real Silent Hill Meetup 2009!++

BBS Signature

Thinking

Cheezie

Reply To Post Reply & Quote

Posted at: 6/2/06 06:33 AM

Cheezie EVIL LEVEL 02

Sign-Up: 03/11/06

Posts: 11

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


None

Impulse

Reply To Post Reply & Quote

Posted at: 8/2/06 11:47 AM

Impulse LIGHT LEVEL 10

Sign-Up: 06/28/06

Posts: 305

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.


Questioning

JackSmack

Reply To Post Reply & Quote

Posted at: 8/2/06 11:57 PM

JackSmack DARK LEVEL 15

Sign-Up: 11/11/04

Posts: 1,155

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

None

Hoeloe

Reply To Post Reply & Quote

Posted at: 8/17/06 07:44 AM

Hoeloe LIGHT LEVEL 28

Sign-Up: 04/29/04

Posts: 5,005

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?

Sex!
------------------------------
Super Nuke Bros. Melee, the web's no. 1 awaited Super Smash Tribute Game!

BBS Signature

None

Hoeloe

Reply To Post Reply & Quote

Posted at: 8/17/06 07:49 AM

Hoeloe LIGHT LEVEL 28

Sign-Up: 04/29/04

Posts: 5,005

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.

Sex!
------------------------------
Super Nuke Bros. Melee, the web's no. 1 awaited Super Smash Tribute Game!

BBS Signature

All times are Eastern Standard Time (GMT -5) | Current Time: 11:55 AM

<< Back

This topic is 2 pages long. [ 1 | 2 ]

<< < > >>
You need a Grounds Gold Account to post on the NG BBS! If you don't have one, click here to sign up now! It's fast, free, and easy — and opens up tons of great NG features!