Skip to main content
Known Participant
August 26, 2011
Question

Drag and Drop Bugs in AIR for iOS

  • August 26, 2011
  • 1 reply
  • 2257 views

Hi guys,

I'm looking for some reliable drag and drop code that works with multiple objects on the stage.

This code works fine on the web, but in AIR I can tear apart my movieclips, so it's unusable:

Does NOT work:

circle_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);
square_mc.addEventListener(MouseEvent.MOUSE_DOWN, drag);


stage.addEventListener(MouseEvent.MOUSE_UP, drop);

function drag(e:MouseEvent):void
{
     e.target.startDrag(false);
   
}

function drop(e:MouseEvent):void
{
     stopDrag();
 
}

This Drag code below works in AIR, but is a bit lengthy. Can anyone help me rewrite it for multiple Movieclips? My AS3 isn't that

good yet. Or if you have a better solution, please post it.

Thanks,

Matt

THIS CODE WORKS IN AIR:

// set listeners
Ghost1.addEventListener(MouseEvent.MOUSE_DOWN, startGhostDrag1);
stage.addEventListener(MouseEvent.MOUSE_UP, stopGhostDrag1);
Ghost1.addEventListener(Event.ENTER_FRAME, dragGhost1);

// offset between sprite location and click
var clickOffset:Point = null;

// user clicked
function startGhostDrag1(event:MouseEvent) {
    clickOffset = new Point(event.localX, event.localY);
   
}

// user released
function stopGhostDrag1(event:MouseEvent) {
    clickOffset = null;
}

// run every frame
function dragGhost1(event:Event) {
    if (clickOffset != null) { // must be dragging
        Ghost1.x = mouseX - clickOffset.x;
        Ghost1.y = mouseY - clickOffset.y;
    }
}

This topic has been closed for replies.

1 reply

Participating Frequently
August 26, 2011

Hi,

I think you are not adding listeners to the right objects. I have taken a variable isDragging and added listener for ENTER_FRAME to stage.

var isDragging:Boolean = false;

// set listeners

Ghost1.addEventListener(MouseEvent.MOUSE_DOWN, startGhostDrag1);

Ghost1.addEventListener(MouseEvent.MOUSE_UP, stopGhostDrag1);

stage.addEventListener(Event.ENTER_FRAME, dragGhost1);

// offset between sprite location and click

//var clickOffset:Point = null;

// user clicked

function startGhostDrag1(event:MouseEvent) {

isDragging = true;

}

// user released

function stopGhostDrag1(event:MouseEvent) {

clickOffset = false;

}

// run every frame

function dragGhost1(event:Event) {

if (isDragging) { // must be dragging

Ghost1.x = mouseX;

Ghost1.y = mouseY;

}

}

Could you try this code?

-Sanika

monkey500Author
Known Participant
August 26, 2011

Thanks for trying Sanika,

but your code didn't work. The first code example I posted works fine, but I'm looking for a simple way of using it for multiple buttons.

Anyone else found bugs with StartDrag code?

Matt

August 26, 2011

By "multiple buttons" do you mean you want to drag more than 1 object using multiple fingers at the same time ?

If yes then you will have to use MultiTouch APIs, use TouchEvent instead of MouseEvent and use startTouchDrag instead of startDrag.

See page : http://www.adobe.com/devnet/flash/articles/multitouch_gestures.html for a tutorial.

Or last example at http://help.adobe.com/en_US/FlashPlatform/beta/reference/actionscript/3/flash/events/TouchEvent.html

Thanks,

Meet