I'm having problems to duplicate movie clips in AS2. Can someone help me to get the general idea how I do this?
Asked
Active
Viewed 1,235 times
1
-
MM both of them. i get the idea of make a "x++" every time when i duplicate. but the problem is that i first open a menu which give the the opportunity to duplicate, so when i open this menu this "x" get again 0. – david Apr 12 '11 at 17:21
-
Ah, ok. Can you post your code? – shanethehat Apr 12 '11 at 17:23
-
'code' this.onRelease=function(){ trace(bedscount); duplicateMovieClip("bed1","bed1"+bedscount,100+bedscount); bedscount++; trace(bedscount); _root["bed1"+bedscount]._x = 480.6; _root["bed1"+bedscount]._y = 450.6; } – david Apr 12 '11 at 17:49
-
Ah, this code is executing within the scope of your button, so the bedscount variable is being newly created every time. On you root timeline declare the variable: `var bedscount:Number = 1;`, then in your function always refer to it using the root scope: `trace(_root.bedscount); _root.bedscount++;` – shanethehat Apr 13 '11 at 08:23
-
oooo ok! one problem solved! thank you very much!! but the bed doesn't appear i think that the problem is in the line: duplicateMovieClip("bed1","bed1"+_root.bedscount,100+_root.bedscount) – david Apr 13 '11 at 10:02
-
Yes, you're trying to use the global version of this function, and that doesn't seem to work as expected, although it also doesn't seem to return an error. Also, you're incrementing the counter and then trying to use it to position the button. Try this instead: `this.onRelease=function(){ this.duplicateMovieClip("bed1"+_root.bedscount,100+_root.bedscount); _root["bed1"+_root.bedscount]._x = 48; _root["bed1"+_root.bedscount]._y = 45; _root.bedscount++; }` – shanethehat Apr 13 '11 at 10:40
-
it doesn't work... maybe the problem is the depth? – david Apr 13 '11 at 10:48
-
It's working in the test I've built, where are you putting the code? – shanethehat Apr 13 '11 at 10:50
-
can I use the function attachMovieClip the same way? – david Apr 13 '11 at 10:51
-
there is a home screen where there is a button 'market' after clicking 'market' a new menu appears. there are beds and each bed is a MC. i put this code in those MCs. When clicking on one of the beds it should appear in the home screen & the menu disappear – david Apr 13 '11 at 10:53
-
attachMovie adds a movieclip from the library to the stage by using its linkage identifier. So if you have a movieclip in the library with the linkage 'bed' then you would attach it to the current stage like this: `this.attachMovie("bed","myNewBed",depth);` – shanethehat Apr 13 '11 at 10:55
-
but in this way I can add only 1 MC' because they all will have the same name. Am I wrong? – david Apr 13 '11 at 11:00
-
Well yeah, that's just a basic example. Of course you will need the name and depth to be unique when creating multiple instances. The problem you have here is that the scope of your application jumps around a bit. This is a massive headache in AS2 if it's not managed carefully. I think what's happening is that the duplicate is being attached to the menu rather than the home. It might help if you can update your question with the code you have on the home and in in the menu. – shanethehat Apr 13 '11 at 11:26
-
it all goes like that: pressing market button - on (release){ _root.attachMovie("shadow","newshadow",200); _root.attachMovie("marketmen","newmarket",300); } – david Apr 21 '11 at 12:26
-
than there is a new menu where we press beds - on (release){ _root.attachMovie("bed_menu","bed_menu2",300); } – david Apr 21 '11 at 12:27
-
than in this menu there are 2 beds. each is a movie clip. when I press on of them it should appear at the house. this.onRelease=function(){ trace(_root.bedscount); duplicateMovieClip("bed1","bed1"+_root.bedscount,100+_root.bedscount); _root.bedscount++; trace(_root.bedscount); _root["bed1"+_root.bedscount]._x = 480.6; _root["bed1"+_root.bedscount]._y = 450.6; – david Apr 21 '11 at 13:00
-
and the beds menu disapear- _root.newshadow.removeMovieClip(); _root.bed_menu2.removeMovieClip(); – david Apr 21 '11 at 13:01
1 Answers
1
First of all, the IDE: Right click on a symbol in the library and select 'Duplicate'.
Now, code:
var duplicate:MovieClip = original.duplicateMovieClip("duplicateClip",this.getNextHighestDepth());
What you're doing here is calling the duplicateMovieClip function of your existing clip, telling it a name and depth for your new clip. It returns a reference to the new clip, which is stored in the duplicate
variable.

shanethehat
- 15,460
- 11
- 57
- 87