Adding multiple movieclips

Im trying to add a movieclip to the stage (inside another movieclip but thats not so important) and I have defined the x and y location. Every time I press a certain button a duplicate will be added to the stage in the same location. Once the movieclip has been added it is free to be moved to any location (this already works fine).

My problem is whenever I press the button to add a 2nd duplicate the first one returns to its original co-ordinates and no duplicate is created. Anyone know why?
I would guess that the line marked blue should be inserted inside the ‘createNewPiece’ function (not 100%) but it needs to remain outside as I have other functions using this variable.

[COLOR=Blue]var Jig:MovieClip = new Jigsaw1();[/COLOR]

template1_mc.addEventListener(MouseEvent.CLICK, createNewPiece, false, 0,
true);

function createNewPiece(evt:MouseEvent):void {
board.addChild(Jig);
Jig.x = goodX(50);
Jig.y = goodY(50);
}

Is there another way or can I place this line within the function and somehow the functions still call the variable?

You’re right - that blue line needs to go inside your ‘createNewPiece’ function. Without it, your ‘createNewPiece’ function simply places your Jig movie clip in its original position. It doesn’t create a *new *movieclip.


var Jig:Jigsaw1; // declare it here so that it's in the scope of other functions

template1_mc.addEventListener(MouseEvent.CLICK, createNewPiece, false, 0,
true);

function createNewPiece(evt:MouseEvent):void {
[COLOR=Blue]Jig = new Jigsaw1(); // create your duplicate here
[/COLOR]board.addChild(Jig);
Jig.x = goodX(50);
Jig.y = goodY(50);
}

The only trouble now is, ‘Jig’ will always refer to the latest copy that’s been made. You don’t have a simple way of referring to the earlier copies. If that’s likely to be a problem, then you could always push the ‘Jigs’ into an array as soon as you create them


var Jig:Jigsaw1;
var clipArray:Array = new Array(); // create an array
template1_mc.addEventListener(MouseEvent.CLICK, createNewPiece, false, 0,
 true);
 
function createNewPiece(evt:MouseEvent):void {
[COLOR=Blue]Jig = new Jigsaw1();
[/COLOR]board.addChild(Jig);
Jig.x = goodX(50);
Jig.y = goodY(50);
clipArray.push(Jig);
 }
 

Now you can refer to clipArray[0], clipArray[1] etc if you need to.

Looks good, I’ll try it, it may solve an upcoming issue also as I will need to reference them in the future.

Thanks alot.