I have been working with a very simple tutorial over at http://monkeyflash.com/tutorials/flash-drag-and-drop/, where you drag objects onto a target movieClip.
My question is this, how do you target a movieClip inside a movie clip?
What I have are 4 color squares that I want to drag and drop onto a pattern_mc. I put the target mc’s with in the pattern_mc. In the tutorial the target mc’s are not in a movie clip.
I put my target mc’s in a movieClip because I want to use the pattern_mc as a material, and I thought this was the correct way to this. Maybe it’s not. Anyway I could use some help.
//record objects starting position
var startX:Number;
var startY:Number;
//activate objects to be moved
square1mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
square1mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
square2mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
square2mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
square3mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
square3mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
square4mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
square4mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
//assign drag and drop functions to red square
function pickUp(event:MouseEvent):void {
event.target.startDrag(true);
//set depth of object higher so when draging it appears over all items
//stacking order
event.target.parent.addChild(event.target);
//capture starting positions
startX = event.target.x;
startY = event.target.y;
}
function dropIt(event:MouseEvent):void {
event.target.stopDrag();
//identify target we just dropped
var myTargetName:String = "target" + event.target.name;
var myTarget:DisplayObject = getChildByName(myTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget) {
//remove event listeners when object is correctly placed
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
event.target.buttonMode = false;
//make sure the objects position is correctly placed
event.target.x = myTarget.x;
event.target.y = myTarget.y;
} else {
//reset position if object is dropped on incorrect placement
event.target.x = startX;
event.target.y = startY;
}
}
square1mc.buttonMode = true;
square2mc.buttonMode = true;
square3mc.buttonMode = true;
square4mc.buttonMode = true;