PDA

View Full Version : Duplication Movie Clip ...


illangoo
10-06-2008, 04:37 AM
Hi,

How to duplicate Movie Clip in AS3,

Thanks

fx.barrett
10-06-2008, 04:58 AM
You simply instantiate your object as many times you want and add it to the display list:

var yourMc:MyMC;

// add yourMc 3 times to the display list
for (var i:int = 0; i < 3; i++)
{
yourMc = new MyMC();
addChild(yourMc);
}

Sekhar
10-06-2008, 05:16 AM
FX, I think he's referring to the AS2 duplicateMovieClip(). Illangoo, unfortunately there's no equivalent in AS3. Lots of people are mad about this, hopefully Adobe will add it back in the next rev.

For now, you'll have manually re-create each copy or design your app differently. I believe there're some third party libraries to simplify the process however - you might want to do a search on these forums and on the Net.

fx.barrett
10-06-2008, 05:20 AM
I was referring to the same thing... That's how you "duplicate" an object in AS 3.0.

illangoo
10-06-2008, 05:37 AM
ok.for duplication i am creating new instance but how can i delete that movie clip from stage

fx.barrett
10-06-2008, 05:46 AM
You could delete it by it's index or even better, by it's name.
Just copy paste these pieces of code and see how they work.

Example 1:

for (var i:int = 0; i < 3; i++)
{
var sp:Sprite = new Sprite();

sp.graphics.beginFill(0xFF0000, 1);
sp.graphics.drawRect(0,0,50,50);
sp.graphics.endFill();

sp.buttonMode = true;
sp.x = (sp.width + 10) * i;

sp.addEventListener(MouseEvent.CLICK, clickHandler);

addChild(sp);
}

function clickHandler(event:MouseEvent):void
{
event.target.parent.removeChild(event.target);
}

Example 2:

for (var i:int = 0; i < 3; i++)
{
var sp:Sprite = new Sprite();

sp.graphics.beginFill(0xFF0000, 1);
sp.graphics.drawRect(0,0,50,50);
sp.graphics.endFill();

sp.name = "sp" + i;
sp.x = (sp.width + 10) * i;

addChild(sp);
}

var remBtn:Sprite = new Sprite();
remBtn.graphics.beginFill(0x000000, 1);
remBtn.graphics.drawRect(0,0,100,50);
remBtn.graphics.endFill();
remBtn.y = 100;
remBtn.buttonMode = true;
remBtn.addEventListener(MouseEvent.CLICK, clickHandler);
addChild(remBtn);

function clickHandler(event:MouseEvent):void
{
removeChild(Sprite(getChildByName("sp2")));
}

Example 3:

for (var i:int = 0; i < 3; i++)
{
var sp:Sprite = new Sprite();

sp.graphics.beginFill(0xFF0000, 1);
sp.graphics.drawRect(0,0,50,50);
sp.graphics.endFill();

sp.x = (sp.width + 10) * i;

addChild(sp);
}

var remBtn:Sprite = new Sprite();
remBtn.graphics.beginFill(0x000000, 1);
remBtn.graphics.drawRect(0,0,100,50);
remBtn.graphics.endFill();
remBtn.y = 100;
remBtn.buttonMode = true;
remBtn.addEventListener(MouseEvent.CLICK, clickHandler);
addChild(remBtn);

function clickHandler(event:MouseEvent):void
{
removeChildAt(2);
}
EDIT: In most of the situations it's recommended that you store each object in an array. So, once you use that for loop to duplicate a symbol you should have an empty array somewhere and with each loop you should push the current object into the empty array. That way, if you ever decide to delete all the objects at once or move them all at once then you can simply loop trough the Array and manipulate all the objects in one fly.

Fast example:

var mcArray:Array = new Array();

for (var i:int = 0; i < 3; i++)
{
var sp:Sprite = new Sprite();

sp.graphics.beginFill(0xFF0000, 1);
sp.graphics.drawRect(0,0,50,50);
sp.graphics.endFill();

sp.x = (sp.width + 10) * i;

mcArray.push(sp);
addChild(sp);
}

var remBtn:Sprite = new Sprite();
remBtn.graphics.beginFill(0x000000, 1);
remBtn.graphics.drawRect(0,0,100,50);
remBtn.graphics.endFill();
remBtn.y = 100;
remBtn.buttonMode = true;
remBtn.addEventListener(MouseEvent.CLICK, clickHandler);
addChild(remBtn);

function clickHandler(event:MouseEvent):void
{
for (var i:int = 0; i < mcArray.length; i++)
{
removeChild(Sprite(mcArray[i]));
}
}

Sekhar
10-06-2008, 06:11 AM
I was referring to the same thing... That's how you "duplicate" an object in AS 3.0.

Not really, because you don't capture changes made to the clip after creation, like scaling, effects, etc. The AS2 duplicateMovieClip() used to replicate the clip along with all the changes. There is no equivalent in AS3.

fx.barrett
10-06-2008, 06:18 AM
True, there is no function that can do that on it's own but it doesn't mean that it can't be done. Only difference is that you must use an array in AS 3.0 to reflect any changes to all the movie clips and I don't see anything wrong with this method.

I honestly am glad that duplicateMovieClip() is gone, it was quite limited and useless, one has much more control over the objects the way I showed then having to use a certain function that does what it does even if you don't want all your objects to behave the same way.