Skip to main content
Known Participant
January 29, 2011
Answered

duplicateMovieClip PLEASE!!!!

  • January 29, 2011
  • 2 replies
  • 1356 views

Hi folks,

First, let me say, I have searched a bit on the internet, but answers I've found just don't make sense to me. And I've tried to search this forum as well, but the search here isn't working for me for some reason.

I have a movie clip on the stage, for our purposes, we'll call it "thing1_mc". I want to duplicate it. How do I do this:

duplicateMovieClip(thing1_mc, "thing2_mc", newDepth)

in AS3? The few places I've seen on the internet are drawn out convoluted explanations, and I'm a moron, so if anyone can give me actual clear, code with simple explanation using small words, you get the big prize.

Thanks in advance,

dp

This topic has been closed for replies.
Correct answer Ned Murphy

Assuming you meant to use "i" instead of "1", try:

for (var i:int=1; i<=12; ++i) {

     this["thing"+String(i)+"_mc"] = new Thing();

     addChild(this["thing"+String(i)+"_mc"]);

}

2 replies

Darshan_Rane
Inspiring
January 29, 2011

Check this

http://snipplr.com/view/12768/duplicatemovieclip-replacement/

------

http://www.darshanrane.com

Ned Murphy
Legend
January 29, 2011

Since it was mentioned that search efforts resulted in only confusing results, and senocular's solution turns up in most every search result, I concluded that would be what was determined to be confusing to implement and understand for the OP.  So without you providing an explanation on how to implement it, it will likely remain confusing... though I could be wrong.

Dan74Author
Known Participant
January 29, 2011

Thanks for the link Darshan, but yeah, the sinocular stuff is a bit heavy for me. I'm sure if I dig into it, I'd get it, but it seems overkill for my needs.

It never occurred to me that I wouldn't need the "var" and the ":Thing" before the equals. Why is it that I don't need those when building the variable dynamically, but when building it "manually" I do? In other words:

Why do I need them here:

     var thing2_mc:Thing = new Thing();

and not here:

     this["thing"+String(i)+"_mc"] = new Thing();

If the answer is "that's just the way AS3 is", I'm cool with that. Although, for a language that's supposed to be more strict, that seems pretty loose.

Thanks for the input, starting to get the hang of it

dp

Ned Murphy
Legend
January 29, 2011

To keep it as simple as you desire, there is no simple way to do what you want in AS3.  There are some fairly complex approaches to doing this, but they would not qualify as being simple to explain or understand.

So the easiest way to get what you want is to create new instances by assigning a class id to the object in the library.

To do that you right click the object in the library and select Linkage from the menu (much the same way you would using attachMovie in AS2).  Then in the linkage interface you select Export for Actionscript and allow whatever else gets checked to get checked automatically.  Then fill in the field labeled "Class:" with a class name... let's say you name it "Thing"

When you want to create a new instance of it, you just use...

var thing2_mc:Thing = new Thing();

addChild(thing2_mc);

Dan74Author
Known Participant
January 29, 2011

Ned, you've come to my rescue again. That sounds more akin to "attachMovieClip", which I get. So that makes sense, and got it working.

However, let's say I wanted to make 12 of them immediately, my inclination is to use a for loop along these lines:

for (var i:int=1; i<=12; ++i) {

     var this["thing"+1+"_mc"]:Thing = new Thing();

     addChild(this["thing"+1+"_mc"]);

}

but it's not working, says it's expecting an identifier before "this". How can I dynamically crate 12 of these w/out using square brackets or building them individually?
thanks,
dp

Ned Murphy
Ned MurphyCorrect answer
Legend
January 29, 2011

Assuming you meant to use "i" instead of "1", try:

for (var i:int=1; i<=12; ++i) {

     this["thing"+String(i)+"_mc"] = new Thing();

     addChild(this["thing"+String(i)+"_mc"]);

}