Thunderforge
Thunderforge

Reputation: 20575

Drag Won't Stop with ActionScript 3

In writing an ActionScript 3 program, I am trying to make it so that I can drag an object from one location to another after doing some other stuff. The trouble is, I can drag just fine, but apparently I can't stop dragging, even with a listener to make it stop. I can't figure out any reason why this is happening. The relevant part of my code is as follows:

public function setToDragAndDrop(){
    this.graphic.removeEventListener(MouseEvent.CLICK, rotate);
    this.graphic.addEventListener(MouseEvent.MOUSE_DOWN, drag);
    this.graphic.addEventListener(MouseEvent.MOUSE_UP, endDrag);
}

private function drag(e:MouseEvent):void{
    trace("Dragging...");
    this.graphic.startDrag(true);
    trace(this.graphic.hasEventListener(MouseEvent.MOUSE_UP)); //Returns true
}

private function endDrag(e:MouseEvent):void{
    trace("Stopped dragging.");
    this.graphic.stopDrag();
}

I do not get the "Stopped dragging" line in my output, so the endDrag MouseEvent is never getting called, even though it exists (as the trace proves). So instead whether the mouse is up or down, it continues to drag. I've found that if I click on another draggable object, that one starts dragging instead.

I'm baffled. Any help would be greatly appreciated!

Upvotes: 1

Views: 1361

Answers (2)

Thunderforge
Thunderforge

Reputation: 20575

I figured out the problem. Apparently when you create the shape, it's "drag point" is at (0,0), or if you premake the shape in Flash, the upper left corner of the "rectangle" that surrounds the figure. When you click on the shape, your mouse jumps to this "drag point."

Apparently, if the drag point does not actually lie on the body of the shape, drag and drop doesn't work. So the ovals I was using earlier didn't work because the drag point was off the shape. Your example did work because it was created at (0,0) and your drag point was at (0,0). But if you draw a circle that isn't at (0,0), like drawCircle(200,200,100) (i.e. draw a circle at point (200,200) with a radius of 100), then you encounter the problem that I have because the drag point is not on the shape.

The solution then is to create the circle at 0,0, then move it to the position you want. It's strange, but I finally got it to work.

Upvotes: 1

mpm
mpm

Reputation: 20155

public function setToDragAndDrop(){
    this.graphic.removeEventListener(MouseEvent.CLICK, rotate);
    this.graphic.addEventListener(MouseEvent.MOUSE_DOWN, drag);
}

private function drag(e:MouseEvent):void{
    this.graphic.startDrag(true);
    this.graphic.removeEventListener(MouseEvent.MOUSE_DOWN, drag);
    this.graphic.addEventListener(MouseEvent.MOUSE_UP, endDrag);
}

private function endDrag(e:MouseEvent):void{
    this.graphic.stopDrag();
    this.graphic.removeEventListener(MouseEvent.MOUSE_UP, endDrag);
    this.graphic.addEventListener(MouseEvent.MOUSE_DOWN, drag);

}

it works very well here.

http://wonderfl.net/c/CvWO

Check the flash "fiddle" .

Upvotes: 0

Related Questions