A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: Assistance With Drag and Drop Problem

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    5

    Assistance With Drag and Drop Problem

    Hi,
    I've been given the AS3 code in the attached zip file (CS4) and the movie clips are not dragging. I want them to drag, drop, and evaluate if they are in the correct drop target.

    Any help would be greatly appreciated.
    Attached Files Attached Files

  2. #2
    Junior Member
    Join Date
    Oct 2011
    Posts
    5

    Assistance With Drag and Drop Issue

    Hi,
    I have this code now and the movie clips are draggable, but they do not release. Can someone please let me know what I'm doing wrong, it's probably something very easy!

    import flash.display.MovieClip;

    // you need to create the variables
    var xstart:Number = 0;
    var ystart:Number = 0;
    var mcAnswer:MovieClip;

    a1_mc.addEventListener(MouseEvent.MOUSE_DOWN,onePr ess);
    a2_mc.addEventListener(MouseEvent.MOUSE_DOWN,onePr ess);
    a3_mc.addEventListener(MouseEvent.MOUSE_DOWN,onePr ess);
    a4_mc.addEventListener(MouseEvent.MOUSE_DOWN,onePr ess);
    a5_mc.addEventListener(MouseEvent.MOUSE_DOWN,onePr ess);
    a6_mc.addEventListener(MouseEvent.MOUSE_DOWN,onePr ess);
    a7_mc.addEventListener(MouseEvent.MOUSE_DOWN,onePr ess);
    a8_mc.addEventListener(MouseEvent.MOUSE_DOWN,onePr ess);
    a9_mc.addEventListener(MouseEvent.MOUSE_DOWN,onePr ess);
    a10_mc.addEventListener(MouseEvent.MOUSE_DOWN,oneP ress);
    a11_mc.addEventListener(MouseEvent.MOUSE_DOWN,oneP ress);
    a12_mc.addEventListener(MouseEvent.MOUSE_DOWN,oneP ress);
    a13_mc.addEventListener(MouseEvent.MOUSE_DOWN,oneP ress);
    a14_mc.addEventListener(MouseEvent.MOUSE_DOWN,oneP ress);
    a15_mc.addEventListener(MouseEvent.MOUSE_DOWN,oneP ress);
    a16_mc.addEventListener(MouseEvent.MOUSE_DOWN,oneP ress);

    function onePress(evt:MouseEvent):void{
    /*
    evt.currentTarget refers to the object
    on the EventListeners which has trigger
    the event.

    so for example if answer2_mc was pressed.
    then the press is the event being triggered
    and answer2_mc is what currentTarget
    would be refering to.
    */
    evt.currentTarget.startDrag(true);
    setChildIndex(evt.currentTarget,numChildren-1);
    textField.text="";
    xstart = evt.currentTarget.x;
    ystart = evt.currentTarget.y;
    }

    a1_mc.addEventListener(MouseEvent.MOUSE_UP,oneRele ase);
    a2_mc.addEventListener(MouseEvent.MOUSE_UP,oneRele ase);
    a3_mc.addEventListener(MouseEvent.MOUSE_UP,oneRele ase);
    a4_mc.addEventListener(MouseEvent.MOUSE_UP,oneRele ase);
    a5_mc.addEventListener(MouseEvent.MOUSE_UP,oneRele ase);
    a6_mc.addEventListener(MouseEvent.MOUSE_UP,oneRele ase);
    a7_mc.addEventListener(MouseEvent.MOUSE_UP,oneRele ase);
    a8_mc.addEventListener(MouseEvent.MOUSE_UP,oneRele ase);
    a9_mc.addEventListener(MouseEvent.MOUSE_UP,oneRele ase);
    a10_mc.addEventListener(MouseEvent.MOUSE_UP,oneRel ease);
    a11_mc.addEventListener(MouseEvent.MOUSE_UP,oneRel ease);
    a12_mc.addEventListener(MouseEvent.MOUSE_UP,oneRel ease);
    a13_mc.addEventListener(MouseEvent.MOUSE_UP,oneRel ease);
    a14_mc.addEventListener(MouseEvent.MOUSE_UP,oneRel ease);
    a15_mc.addEventListener(MouseEvent.MOUSE_UP,oneRel ease);
    a16_mc.addEventListener(MouseEvent.MOUSE_UP,oneRel ease);

    function oneRelease(evt:MouseEvent):void{
    evt.currentTarget.stopDrag();
    switch(evt.currentTarget){
    case a1_mc:
    mcAnswer=benefit2011001_mc;
    break
    case a2_mc:
    mcAnswer=benefit2011001_mc;
    break
    case a3_mc:
    mcAnswer=benefit2011001_mc;
    break
    case a4_mc:
    mcAnswer=benefit2011001_mc;
    break
    case a5_mc:
    mcAnswer=benefit2011002_mc;
    break
    case a6_mc:
    mcAnswer=benefit2011002_mc;
    break
    case a7_mc:
    mcAnswer=benefit2011002_mc;
    break
    case a8_mc:
    mcAnswer=benefit2011002_mc;
    break
    case a9_mc:
    mcAnswer=benefit2011003_mc;
    break
    case a10_mc:
    mcAnswer=benefit2011003_mc;
    break
    case a11_mc:
    mcAnswer=benefit2011003_mc;
    break
    case a12_mc:
    mcAnswer=benefit2011003_mc;
    break
    case a13_mc:
    mcAnswer=allPlans_mc;
    break
    case a14_mc:
    mcAnswer=allPlans_mc;
    break
    case a15_mc:
    mcAnswer=allPlans_mc;
    break
    case a16_mc:
    mcAnswer=allPlans_mc;
    break
    }

    if (evt.currentTarget.hitTestObject(mcAnswer)) {
    textField.text="Correct!";
    /*
    numbers aren't strings so we convert it
    to a string so we can use it for the textfield
    */
    evt.currentTarget.enabled = false;
    }
    else {
    textField.text="Incorrect.";
    evt.currentTarget.x = xstart;
    evt.currentTarget.y = ystart;
    }
    }

  3. #3
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Try using event.target instead of currentTarget.
    Please use [php] or [code] tags, and mark your threads resolved 8)

  4. #4
    Junior Member
    Join Date
    Oct 2011
    Posts
    5

    Assistance With Drag and Drop Issue

    In all functions, or in one particular?

    Thanks!!

  5. #5
    Ө_ө sleepy mod
    Join Date
    Mar 2003
    Location
    Oregon, USA
    Posts
    2,441
    Try all instances of currentTarget - an event's target is going to be the thing you attached the listener to, while the currentTarget can be any children of that object that you might have clicked. In practice you'll usually want the target since it will predictably be the thing you were interfacing with when you added the listener.

    You might also get the same result by setting mouseChildren to false for those draggable movieclips.
    Please use [php] or [code] tags, and mark your threads resolved 8)

  6. #6
    Junior Member
    Join Date
    Oct 2011
    Posts
    5

    Assistance With Drag and Drop Problem

    Hi,
    The movie clips are not dropping, for some reason still.
    Attached Files Attached Files
    Last edited by rhanna2; 10-10-2011 at 09:47 AM.

  7. #7
    Senior Member
    Join Date
    Jul 2001
    Location
    San Jose California
    Posts
    144
    unsure if this is helpful but when I do a scrollbar, which is somewhat similar I guess, I put the mouse up listener on the stage

    - stage.addEventListener(MouseEvent.MOUSE_UP, whatever);

  8. #8
    Senior Member
    Join Date
    Jul 2008
    Posts
    391
    I suspect the registration points for your MovieClips are not in the center but rather in the corner of the MovieClips. I just did a test where I had a square MovieClip with the registration point being at (0, 0) and it won't release the drag, but once I move the point onto the actual graphics it works.

    Quote Originally Posted by neznein9 View Post
    Try all instances of currentTarget - an event's target is going to be the thing you attached the listener to, while the currentTarget can be any children of that object that you might have clicked.
    I thought it was the other way around? When I have a MovieClip with a TextField and a colored background both the same size so the TextField technically covers the whole MovieClip, e.target returns the TextField and e.currentTarget returns the MovieClip.

  9. #9
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    You're right. currentTarget is the item the listener was added to, while target may be a descendant.

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