With some excellent help from ilike2 I was able to import random objects from a library that can then be dragged to their respective targets (on repeat).
However, I forgot to mention that I need to drag more than one of these objects onto the same target and I don’t know how to change the actionscript to accommodate this.
Would anyone be able to help me with this?
In the example below I would also like Blue2 to target targetBlue_mc
Here is the code:
var movieArray:Array = new Array(new Red(), new Green(), new Blue(), new Blue2());
var targetArray:Array = new Array(targetRed_mc, targetGreen_mc, targetBlue_mc );
var tempMc:MovieClip;
var r:int;
function init():void{
r = Math.random() * movieArray.length;
tempMc = movieArray[r];
tempMc.x = 250;
tempMc.y = 50;
tempMc.buttonMode = true;
tempMc.arrayIndex = r;
tempMc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
tempMc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
addChild(tempMc);
}
function pickUp(e:MouseEvent):void {
e.target.startDrag(true);
}
function dropIt(e:MouseEvent):void{
e.target.stopDrag();
if (e.target.hitTestObject(targetArray[e.target.arrayIndex])) {
e.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
e.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
e.target.buttonMode = false;
e.target.x = targetArray[e.target.arrayIndex].x;
e.target.y = targetArray[e.target.arrayIndex].y;
setTimeout(startAgain, 500)
}
}
function startAgain():void{
removeChild(tempMc);
init();
}
init();
Also attached is the test file