I was thinking this is a simple task, but I\'m wrong.
I used a sprite to display an image, and when user drag it(MOUSE_DOWN and MOUSE_MOVE), I got the position in
Here, try this. It's just a simple black square, but it looks fine until you really start dragging it around. As was mentioned, setting the framerate to something higher is ideal. In this case, I decided to up the framerate to 60fps in the MOUSE_DOWN and drop it back to 24 in MOUSE_UP for memory reasons. You can obviously change that how you please.
import flash.display.*;
import flash.events.*;
var startX:Number;
var startY:Number;
var shape:Sprite = new Sprite();
shape.graphics.beginFill(0x000000);
shape.graphics.drawRect(0,0,50,50);
shape.graphics.endFill();
this.addChild(shape);
shape.addEventListener(MouseEvent.MOUSE_DOWN,this.mouseDown);
function mouseDown(e:MouseEvent = null):void{
stage.frameRate = 60;
startX = stage.mouseX - shape.x;
startY = stage.mouseY - shape.y;
stage.addEventListener(MouseEvent.MOUSE_MOVE,this.mouseMove);
shape.addEventListener(MouseEvent.MOUSE_UP,this.mouseUp);
}
function mouseMove(e:MouseEvent = null):void{
shape.x = stage.mouseX - startX;
shape.y = stage.mouseY - startY;
}
function mouseUp(e:MouseEvent = null):void{
shape.removeEventListener(MouseEvent.MOUSE_UP,this.mouseUp);
stage.removeEventListener(MouseEvent.MOUSE_MOVE,this.mouseMove);
stage.frameRate = 24;
}
Make sure you are removing the MOUSE_MOVE event on MOUSE_UP. That is key. Otherwise, you re-add the event on every MOUSE_DOWN and end up running the same code repeatedly, simultaneously. Sorry my syntax isn't 100% proper; I threw this together really quick in CS5.5 rather than doing it in Flash Builder.