A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Drag and Drop game with only two targets

  1. #1
    Junior Member
    Join Date
    Apr 2009
    Posts
    19

    Drag and Drop game with only two targets

    I created a drag and drop activity where their are five boxes that are supposed to go in to two columns in any order. I used this tutorial to build it http://www.flashclassroom.com/docs/d...tsflashcs3.pdf

    The problem is that the tutorial shows you how to make a target for each mc which has worked for all the activities I've made except for this one where I need only two targets. How would I change it so I can have multiple mc's drop on to one target?

    This is my AS:

    var score:Number = 0;

    var objectoriginalX:Number;
    var objectoriginalY:Number;

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

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

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

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

    hosttrans_mc.buttonMode = true;
    hosttrans_mc.addEventListener(MouseEvent.MOUSE_DOW N, pickupObject);
    hosttrans_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 matchingTargetisplayObject = 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;
    score++;
    scoreField.text = String(score);

    } else {
    event.target.x = objectoriginalX;
    event.target.y = objectoriginalY;
    }
    if(score == 5){
    response_mc.gotoAndStop(2);
    }
    }
    Last edited by good_snow; 06-08-2012 at 07:07 PM.

  2. #2
    Flash/Flex Developer samac1068's Avatar
    Join Date
    Apr 2007
    Location
    Here, no there
    Posts
    1,813
    When you are comparing your drop target you can use the same MC. I usually place my object list in an array with it's drop target and when I do a hitTest I can compare from that list.
    Some people are like Slinkies, not really good for anything, but they bring a smile to your face when pushed down the stairs.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center