/ Published in: ActionScript 3
Using StartDrag can be problematic and buggy in Flash. I've found that sometimes you don't get the position of the object your dragging updated and it can really mess with any tweens and effects you apply on the object you are dragging afterward. Manually dragging an object is usually better. This code will do the job for you.
There's a lot of extra baggage in here but you can see the general point of it.
There's a lot of extra baggage in here but you can see the general point of it.
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
//------------------------------------------------------------------------------------ private function startDragHandler(e:MouseEvent = null):void { /* Set custom cursor */ //if(!dragCursor) dragCursor = new DragCursor(); CursorManager.setCursor(DragCursor as Class,CursorManagerPriority.HIGH,0,0); isMouseOffStage = false; this.stage.addEventListener(Event.MOUSE_LEAVE,mouseLeftStage); this.stage.addEventListener(MouseEvent.MOUSE_OUT,mouseOutStage); this.stage.addEventListener(MouseEvent.MOUSE_OVER,mouseOverStage); this.stage.addEventListener(MouseEvent.MOUSE_UP,stopDragHandler); if(DataModel.getInstance().isAllowDragShirt) { moveFromPointX = this.mouseX; moveFromPointY = this.mouseY; addEventListener(Event.ENTER_FRAME,draggingHandler); } } //------------------------------------------------------------------------------------ private function draggingHandler(e:Event = null):void { var targetX:Number = this.x+(this.mouseX-moveFromPointX); var targetY:Number = this.y+(this.mouseY-moveFromPointY); if(targetX>0 || targetX<maxMoveX) targetX = this.x; if(targetY>0 || targetY<=maxMoveY) targetY = this.y; this.x = targetX; this.y = targetY; moveFromPointX = this.mouseX; moveFromPointY = this.mouseY; } //------------------------------------------------------------------------------------ private function stopDragHandler(e:MouseEvent = null):void { moveFromPointX=this.x; moveFromPointY=this.y; CursorManager.removeAllCursors(); if(DataModel.getInstance().isBusy) CursorManager.setBusyCursor(); this.stage.removeEventListener(Event.MOUSE_LEAVE,mouseLeftStage); this.stage.removeEventListener(MouseEvent.MOUSE_OUT,mouseOutStage); this.stage.removeEventListener(MouseEvent.MOUSE_OVER,mouseOverStage); this.removeEventListener(Event.ENTER_FRAME,draggingHandler); //stopDrag(); }