Skip to main content
Participant
July 16, 2010
Question

Drag and Drop Multiple items to One Target

  • July 16, 2010
  • 1 reply
  • 1415 views

Hi,

As a lot of people say, I'm quite new to actionscript and what I've accomplished thus far has been from trial and error. But I've hit a wall. I'm trying to create an interactive game where multiple movieclips can be dragged onto a single target. I understand that the target and the movie clip have to have the same name, like "book_mc" has to have a target of "targetbook_mc", but is there another way to do this so that "book_mc" and "pencil_mc" can both have the same target?

I'm open to all suggestions. I'd post my code, but I don't event think I'm anywhere on the right track.

Thanks so much!

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
July 16, 2010

you're mistaken.  the droptarget can have any name you like.  there need not be an obvious relationship between the dragged movieclip and its desired droptarget.

(that said, it does simplify programming if there's a consistant relationship between the dragged movieclip's name and its desired droptarget name.)

anyway, your two movieclips can use the same droptarget.  just make sure a previously dropped movieclip does not remain above the droptarget after dragging is stopped or the next drag/dropped movieclip won't detect the desired droptarget.

arayn1124Author
Participant
July 16, 2010

Thanks for the correction. I guess I'm even more of a beginner than I thought. If you have a moment to look at what I have done so far, I'd really appreciate it. Can the z-index of the dropped movie clip be changed or does it need to move completely off the target?

Thanks as well for any input you can provide!

var counter:Number = 0;
var startX:Number;
var startY:Number;

aAG_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
aAG_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
bAG_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
bAG_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
eAG_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
eAG_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
gAG_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
gAG_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
nAG_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
nAG_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);
rAG_mc.addEventListener(MouseEvent.MOUSE_DOWN, pickUp);
rAG_mc.addEventListener(MouseEvent.MOUSE_UP, dropIt);


function pickUp(event:MouseEvent):void {
    event.target.startDrag(true);
    reply_txt.text = "";
    event.target.parent.addChild(event.target);
    startX = event.target.x;
    startY = event.target.y;
}
function dropIt(event:MouseEvent):void {
    event.target.stopDrag();
    var targetaAG_mc:String = "target" + event.target.name;
    var myTarget:DisplayObject = getChildByName(targetaAG_mc);
    if (event.target.dropTarget != null && event.target.dropTarget.parent == myTarget){
        reply_txt.text = "Correct";
        event.target.removeEventListener(MouseEvent.MOUSE_DOWN, pickUp);
        event.target.removeEventListener(MouseEvent.MOUSE_UP, dropIt);
        event.target.buttonMode = false;
        event.target.x = myTarget.x;
        event.target.y = myTarget.y;
        counter++;
    } else {
        reply_txt.text = "Try Again";
        event.target.x = startX;
        event.target.y = startY;
    }
    if(counter == 6){
        reply_txt.text = "You Win!";
    }
}

aAG_mc.buttonMode = true;
bAG_mc.buttonMode = true;
eAG_mc.buttonMode = true;
gAG_mc.buttonMode = true;
nAG_mc.buttonMode = true;
rAG_mc.buttonMode = true;

Inspiring
July 16, 2010

What if you group things into arrays and onDrop check the objects array and not the object itself?

So, you would check if book or pencil are part of the school array, or something.

Well, it's just an idea. You seems to know much more about As3 then the poor me.

Also, event.target.parent.addChild(event.target); is not the same as simply using addChild(event.target)? Uh, maybe not.