mercredi 1 mars 2017

How to create a drag/drop with ActionScript 3.0? Multiple items being dragged to multiple targets

I am creating an interactive paper doll in Adobe Animate and am having some trouble making the drag drop feature work. I have multiple clothing items that will need to have the possibility of being dragged to the same area. For example I may have 4 different shirt options. I have started with the two dresses (yellow dress_mc & blue dress_mc). The yellow dress is working properly, but the blue dress won't work, I can drag it, but not drop it. Any suggestions?

This is the code I have right now:

var objectoriginalX:Number;
var objectoriginalY:Number;

flower_mc.buttonMode = true;
flower_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
flower_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);

yellowdress_mc.buttonMode = true;
yellowdress_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
yellowdress_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);

bluedress_mc.buttonMode = true;
bluedress_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
bluedress_mc.addEventListener(MouseEvent.MOUSE_UP, dropObject);

function pickupObject(event:MouseEvent):void {
event.target.startDrag(true);
event.target.parent.addChild(event.target);
objectoriginalX = event.target.x;
objectoriginalY = event.target.y;
}
function dropObject(event:MouseEvent):void {
event.target.stopDrag();
var matchingTargetName:String = "target" + event.target.name;
var matchingTarget:DisplayObject = getChildByName(matchingTargetName);
if (event.target.dropTarget != null && event.target.dropTarget.parent == 
matchingTarget){
event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickupObject);
event.target.removeEventListener(MouseEvent.MOUSE_UP, dropObject);
event.target.buttonMode = false;
event.target.x = matchingTarget.x;
event.target.y = matchingTarget.y;
} else {
event.target.x = objectoriginalX;
event.target.y = objectoriginalY;
}
}

Aucun commentaire:

Enregistrer un commentaire