I am trying to implement a simple movement for one of game objects in flash/ AS3. I am using mouseX, mouseY for my object to follow the mouse pointer. It works fine but the problem is the precision of the movement. If I move my mouse a little too fast then my game object stops following the mouse which means I have to re-focus the mouse pointer back on the game object to make it move again.

Here is the code that does this.

private function FootyMouseMove(shortestFooty : FootBall) : void {
            shortestFooty.x = stage.mouseX;
            shortestFooty.y = stage.mouseY;
            if (shortestFooty.x > stage.stageWidth - shortestFooty.width)
                shortestFooty.x = stage.stageWidth - shortestFooty.width;
            if (shortestFooty.x < shortestFooty.width)
                shortestFooty.x = shortestFooty.width;

My question is, would it be possible to make the movement of my game object much more smoother and make sure that the game object is always following the mouse no matter how fast I move my mouse pointer?


1 Answer 1


I don't think there's enough information to diagnose the problem, but my guess is that you have the MOUSE_MOVE handler on the object you're dragging. That event fires only when the mouse is over the object. When you move the mouse quickly, it ends up not over that object, so you stop getting the events. This is not a problem with “precision”, but about where you mouse event handlers.

If this is indeed the problem, the solution I've used in the past is to capture events on a larger sprite, such as the stage.

  1. Capture MOUSE_DOWN on the object in question. This is the start of the drag operation.
  2. In the MOUSE_DOWN handler, add MOUSE_MOVE and MOUSE_UP handlers to the stage object. This will let you track the movement until the mouse is released.
  3. In the MOUSE_MOVE handler (on stage), you'll need to translate coordinates from stage to your object's coordinates using localToGlobal or globalToLocal. This is the drag operation.
  4. In the MOUSE_UP handler (on stage), remove MOUSE_MOVE and MOUSE_UP from stage. This is the end of the drag operation.

If you want to track on mouse move even when the mouse button isn't held down, it's even simpler. Put the mouse move handler on the stage object and translate coordinates.


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .