package { import flash.display.Stage; import flash.events.MouseEvent; import flash.events.Event; public class DragObject extends ShadowObject { public var to_x:Number; public var to_y:Number; private var mouseup:Boolean; public var offsetx:Number; public var offsety:Number; public function DragObject(posoffsetx:Number = 0, posoffsety:Number = 0) { offsetx = posoffsetx; offsety = posoffsety; to_x = this.y-offsetx; to_y = this.x-offsety; mouseup = true; this.addEventListener(MouseEvent.MOUSE_DOWN, this.startMove); this.addEventListener("releaseoutside", this.stopMove); } private function startMove(event:MouseEvent):void { mouseup = false; this.addEventListener(Event.ENTER_FRAME, dragUser); } public function stopMove(event:Event):void { mouseup = true; } private function dragUser(event:Event):void { if(!mouseup) { to_x = Math.round(stage.mouseX-offsetx); to_y = Math.round(stage.mouseY-offsety); } if(this.x < to_x) { this.x+= Math.round(Math.abs(this.x-to_x)/5); } else if(this.x > to_x) { this.x-= Math.round(Math.abs(this.x-to_x)/5); } if(this.y < to_y) { this.y+= Math.round(Math.abs(this.y-to_y)/5); } else if(this.y > to_y) { this.y-= Math.round(Math.abs(this.y-to_y)/5); } if(this.y == to_y && this.x == to_x) { this.removeEventListener(Event.ENTER_FRAME, dragUser); } } } }