AS3 startDrag/stopDrag is similar to AS2 except it uses mouse events to detect the mouse movement, below is a simple implementation:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package { import flash.display.*; import flash.events.*; public class DragExample extends Sprite { function DragExample() { addEventListener(MouseEventType.MOUSE_DOWN, onMouseDown); stage.addEventListener(MouseEventType.MOUSE_UP, onMouseUp); } private function onMouseDown(event:Event):Void { this.startDrag(); } private function onMouseUp(event:Event):Void { this.stopDrag(); } } } |
You will need stage to listen to mouse up event in order to stop dragging as you might release your mouse too quick or not on the sprite.
3 users commented in " AS3 startDrag/stopDrag example "
Follow-up comment rss or Leave a Trackbackcould you show an example of how using the startDrag method parameters?
I’ve tried:
object.startDrag(false, object.width / 2, object.height / 2, stage.stageWidth – object.width / 2 , stage.stageHeight – object.height / 2);
with numbers that would be:
object.startDrag(false, 0,0,100,100);
but it doesn’t work, it shows an error.
My goal here is to limit dragging so I don’t show an empty space while exceeding the limit.
Thank you!
You need to use Rectangle class like “object.startDrag(true, new Rectangle(0,0,100,100));”
Yup, need to use the Rectangle object to define the boundaries of the drag:
Rectangle(x:Number = 0, y:Number = 0, width:Number = 0, height:Number = 0)Leave A Reply