Be a Supporter!

As: Using Duplicated Movie Clips

  • 14,997 Views
  • 40 Replies
New Topic Respond to this Topic
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
As: Using Duplicated Movie Clips Jun. 28th, 2005 @ 09:25 AM Reply

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
Dancing-Thunder
Dancing-Thunder
  • Member since: Apr. 19, 2005
  • Offline.
Forum Stats
Member
Level 04
Blank Slate
Response to As: Using Duplicated Movie Clips Jun. 28th, 2005 @ 09:28 AM Reply

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!

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Using Duplicated Movie Clips Jun. 28th, 2005 @ 09:34 AM Reply

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
Siggy0501
Siggy0501
  • Member since: Jan. 8, 2005
  • Offline.
Forum Stats
Member
Level 23
Blank Slate
Response to As: Using Duplicated Movie Clips Jun. 28th, 2005 @ 09:45 AM Reply

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

GeoffCLogan
GeoffCLogan
  • Member since: Apr. 25, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to As: Using Duplicated Movie Clips Jun. 28th, 2005 @ 09:59 AM Reply

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.

Toast
Toast
  • Member since: Apr. 2, 2005
  • Offline.
Forum Stats
Member
Level 09
Blank Slate
Response to As: Using Duplicated Movie Clips Jul. 13th, 2005 @ 11:07 AM Reply

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


BBS Signature
Hoeloe
Hoeloe
  • Member since: Apr. 29, 2004
  • Offline.
Forum Stats
Member
Level 37
Game Developer
Response to As: Using Duplicated Movie Clips Jan. 3rd, 2006 @ 01:02 PM Reply

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


Song of the Firefly is on Steam Greenlight and Facebook. Give them a look and support the project!
------------------------------

BBS Signature
Rookie209
Rookie209
  • Member since: Feb. 20, 2006
  • Offline.
Forum Stats
Member
Level 06
Blank Slate
Response to As: Using Duplicated Movie Clips Mar. 5th, 2006 @ 09:28 PM Reply

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

fenix
fenix
  • Member since: Sep. 7, 2005
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to As: Using Duplicated Movie Clips Mar. 21st, 2006 @ 09:01 PM Reply

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

Pyromaniac
Pyromaniac
  • Member since: Jan. 14, 2005
  • Offline.
Forum Stats
Member
Level 18
Blank Slate
Response to As: Using Duplicated Movie Clips Mar. 21st, 2006 @ 09:50 PM Reply

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.

fenix
fenix
  • Member since: Sep. 7, 2005
  • Offline.
Forum Stats
Member
Level 22
Blank Slate
Response to As: Using Duplicated Movie Clips Mar. 21st, 2006 @ 10:08 PM Reply

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?

Hoeloe
Hoeloe
  • Member since: Apr. 29, 2004
  • Offline.
Forum Stats
Member
Level 37
Game Developer
Response to As: Using Duplicated Movie Clips Apr. 8th, 2006 @ 03:33 AM Reply

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


Song of the Firefly is on Steam Greenlight and Facebook. Give them a look and support the project!
------------------------------

BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Using Duplicated Movie Clips Apr. 8th, 2006 @ 03:53 AM Reply

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
Hoeloe
Hoeloe
  • Member since: Apr. 29, 2004
  • Offline.
Forum Stats
Member
Level 37
Game Developer
Response to As: Using Duplicated Movie Clips Apr. 8th, 2006 @ 03:56 AM Reply

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


Song of the Firefly is on Steam Greenlight and Facebook. Give them a look and support the project!
------------------------------

BBS Signature
Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Using Duplicated Movie Clips Apr. 8th, 2006 @ 03:58 AM Reply

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
Richy-gorgan
Richy-gorgan
  • Member since: Nov. 29, 2005
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to As: Using Duplicated Movie Clips Apr. 8th, 2006 @ 07:21 AM Reply

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?

Richy-gorgan
Richy-gorgan
  • Member since: Nov. 29, 2005
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to As: Using Duplicated Movie Clips Apr. 8th, 2006 @ 07:28 AM Reply

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

Denvish
Denvish
  • Member since: Apr. 25, 2003
  • Offline.
Forum Stats
Member
Level 46
Blank Slate
Response to As: Using Duplicated Movie Clips Apr. 8th, 2006 @ 07:40 AM Reply

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
Richy-gorgan
Richy-gorgan
  • Member since: Nov. 29, 2005
  • Offline.
Forum Stats
Member
Level 19
Blank Slate
Response to As: Using Duplicated Movie Clips Apr. 8th, 2006 @ 07:44 AM Reply

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

Hoeloe
Hoeloe
  • Member since: Apr. 29, 2004
  • Offline.
Forum Stats
Member
Level 37
Game Developer
Response to As: Using Duplicated Movie Clips Apr. 8th, 2006 @ 07:55 AM Reply

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


Song of the Firefly is on Steam Greenlight and Facebook. Give them a look and support the project!
------------------------------

BBS Signature
Hoeloe
Hoeloe
  • Member since: Apr. 29, 2004
  • Offline.
Forum Stats
Member
Level 37
Game Developer
Response to As: Using Duplicated Movie Clips Apr. 8th, 2006 @ 08:40 AM Reply

kmon ppl,i need this help :(


Song of the Firefly is on Steam Greenlight and Facebook. Give them a look and support the project!
------------------------------

BBS Signature
Hoeloe
Hoeloe
  • Member since: Apr. 29, 2004
  • Offline.
Forum Stats
Member
Level 37
Game Developer
Response to As: Using Duplicated Movie Clips Apr. 8th, 2006 @ 08:50 AM Reply

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


Song of the Firefly is on Steam Greenlight and Facebook. Give them a look and support the project!
------------------------------

BBS Signature
Cheezie
Cheezie
  • Member since: Mar. 11, 2006
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to As: Using Duplicated Movie Clips May. 21st, 2006 @ 02:21 AM Reply

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.

shazwoogle
shazwoogle
  • Member since: Sep. 27, 2004
  • Offline.
Forum Stats
Member
Level 11
Blank Slate
Response to As: Using Duplicated Movie Clips May. 21st, 2006 @ 03:23 AM Reply

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.
KLoWnXXX
KLoWnXXX
  • Member since: Feb. 12, 2005
  • Offline.
Forum Stats
Member
Level 16
Blank Slate
Response to As: Using Duplicated Movie Clips May. 30th, 2006 @ 07:50 AM Reply

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
Cheezie
Cheezie
  • Member since: Mar. 11, 2006
  • Offline.
Forum Stats
Member
Level 02
Blank Slate
Response to As: Using Duplicated Movie Clips Jun. 2nd, 2006 @ 06:33 AM Reply

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

Impulse
Impulse
  • Member since: Jun. 28, 2006
  • Offline.
Forum Stats
Member
Level 10
Blank Slate
Response to As: Using Duplicated Movie Clips Aug. 2nd, 2006 @ 11:47 AM Reply

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.

JackSmack
JackSmack
  • Member since: Nov. 11, 2004
  • Offline.
Forum Stats
Member
Level 22
Game Developer
Response to As: Using Duplicated Movie Clips Aug. 2nd, 2006 @ 11:57 PM Reply

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
Hoeloe
Hoeloe
  • Member since: Apr. 29, 2004
  • Offline.
Forum Stats
Member
Level 37
Game Developer
Response to As: Using Duplicated Movie Clips Aug. 17th, 2006 @ 07:44 AM Reply

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?


Song of the Firefly is on Steam Greenlight and Facebook. Give them a look and support the project!
------------------------------

BBS Signature
Hoeloe
Hoeloe
  • Member since: Apr. 29, 2004
  • Offline.
Forum Stats
Member
Level 37
Game Developer
Response to As: Using Duplicated Movie Clips Aug. 17th, 2006 @ 07:49 AM Reply

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.


Song of the Firefly is on Steam Greenlight and Facebook. Give them a look and support the project!
------------------------------

BBS Signature