A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: Drag and drop to multiple targets in AS3

Hybrid View

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    1

    Drag and drop to multiple targets in AS3

    Hi,

    I have created a simple drag and drop game with 4 signs to be dragged onto 4 targets. As two of the answers are the same I need to be able to have the possibility of both signs to be dragged onto either target.

    Below is the code i am using - if anyone can help it would be much appreciated.

    Thanks in advance,

    m

    stop();

    // Second Level Quit button
    // loads second.swf in again

    quit_mc.addEventListener(MouseEvent.CLICK, fl_ClickToLoadUnloadSWF_2);

    //go to next scene button

    forwardTed.addEventListener(MouseEvent.CLICK, fl_ClickToGoToNextScene);


    var dragArray:Array = [Sign30, Sign60, Sign70, Sign70_2 ];
    var matchArray:Array = [Sign30Match, Sign60Match, SignMatch70 || SignMatch70_2 ];


    var currentClip:MovieClip;
    var startX:Number;
    var startY:Number;

    *

    for(var i:int = 0; i < dragArray.length; i++) {

    **dragArray[i].buttonMode = true;**
    **dragArray[i].addEventListener(MouseEvent.MOUSE_DOWN, item_onMouseDown);
    **matchArray[i].alpha = 0.0;

    }

    *

    function item_onMouseDown(event:MouseEvent):void {
    ****currentClip = MovieClip(event.currentTarget);
    ****startX = currentClip.x;
    ****startY = currentClip.y;
    ****addChild(currentClip); //bring to the front
    ****currentClip.startDrag();
    ****stage.addEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);

    }

    function stage_onMouseUp(event:MouseEvent):void {
    ****stage.removeEventListener(MouseEvent.MOUSE_UP, stage_onMouseUp);
    ****currentClip.stopDrag();
    ****var index:int = dragArray.indexOf(currentClip);
    ****var matchClip:MovieClip = MovieClip(matchArray[index]);
    ****if(currentClip.hitTestObject(matchClip)) {

    ********//a match was made! position the clip on the matching clip:
    ********currentClip.x = matchClip.x;
    ********currentClip.y = matchClip.y;
    ********//make it not draggable anymore:
    ********currentClip.removeEventListener(MouseEvent .MOUSE_DOWN, item_onMouseDown);
    * currentClip.buttonMode = false;

    ****} else {

    ********//match was not made, so send the clip back where it started:
    currentClip.x = startX;
    currentClip.y = startY;

    ****}

    }

  2. #2
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    You'll have to do it explicitly...

    PHP Code:
        if(index == || index == 3){
            
    //  first two clips are the interchangeable ones
            
    if(currentClip.hitTestObject(matchArray[2])){
                
    //  place it on match 2
            
    } else if(currentClip.hitTestObject(matchArray[3])){
                
    //  place it on match 3
            
    } else {
                
    //  fail - return to origin
            
    }
        } else {
            
    //  in this block - we're using one of the unique clips
            
    if(currentClip.hitTestObject(matchClip)){
                
    //  place it correctly
            
    } else {
                
    //  fail - return to origin
            
    }
        } 
    Please use [php] or [code] tags, and mark your threads resolved 8)

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