Changes in Distance


I am looking for a way to measure changes in distance between my character sprite and the mouse and whether that change is an increase and decrease. This will make it easier for me to implement backward and forward animations.

I imagine that flashpunk has some neat tools to make something like this easier, but I don’t know where to start looking. Any suggestions?

(Martí Angelats i Ribera) #2

I think we need to know a bit more about the mechanic you want to implement.

(Jacob Albano) #3

On your class:

private var lastDistance:Number = 0;

In your update:

var distance:Number = FP.distance(character.x, character.y, mouseX, mouseY);
if (distance > lastDistance)
    // increased
else if (distance < lastDistance)
    // decreased

lastDistance = distance;


Topdown shooter. Character rotates and shoots to mouse. As the character backs away from the mouse, a back walking animation plays. As character approaches mouse, a forward walking animation plays. When the character is neither moving toward or away from the mouse, neither animation plays.

(Zachary Lewis) #5

@jacobalbano’s solution was pretty good, but if you’d like to determine if the move you’re about to make is away or toward the mouse in the current frame, you can compare the current position of the character to the destination of the character.

// The distances the character will move this frame.
var mx:Number, my:Number;

// #TODO: Determine how far the character will move based on inputs.

// Compare distances to see if the character is moving toward or away from the mouse.
var dCurrent:Number = FP.distance(character.x, character.y, mouseX, mouseY);
var dTarget:Number = FP.distance(character.x + mx, character.y + my, mouseX, mouseY);

if (dCurrent > dTarget)
  // The character is moving toward the mouse.
else if (dCurrent < dTarget)
  // The character is moving away from the mouse.
  // The character's distance from the mouse isn't changing.
  // The character is probably not moving.

// Handle character movement.
character.x += mx;
character.y += my;


I did exactly as you said and tried tweaking it a few times. I couldn’t get it to work. Undoubtedly an error on my part, but looking at your code, it seems that, should the mouse move towards the character, the walking animation will trigger. I only want the animation to play when the character moves, not when the change in distance is caused by the mouse being moved toward the character. Jacob’s solution is good; it helps me a lot. Of course, it still leaves the problem of determining when the character moves. This is what I have tried to do:

public class Player extends Entity{

public var sprPlyr:Spritemap = new Spritemap(Assets.Plyr, 128, 128);

private var lastDistance:Number = 0

private var moveDir:Number = 0

private var moveKeyPressed:Boolean = false

public function Player()


    sprPlyr.add("walk", [0, 1, 2, 3, 4, 5, 6, 7], 20, true);

    graphic = sprPlyr;



override public function update(): void 


    if (Input.check(Key.A)) { x -= 2 };

    if (Input.check(Key.D)) { x += 2 };

    if (Input.check(Key.W)) { y -= 2 };

    if (Input.check(Key.S)) { y += 2 };


    if (Input.pressed(Key.A)) { moveKeyPressed = true }

    else if (Input.pressed(Key.D)) { moveKeyPressed = true }

    else if (Input.pressed(Key.W)) { moveKeyPressed = true }

    else if (Input.pressed(Key.S)) { moveKeyPressed = true }

    else (moveKeyPressed = false)


    sprPlyr.originX = sprPlyr.width / 2;

    sprPlyr.originY = sprPlyr.height / 2;

    sprPlyr.angle = FP.angle(Input.mouseX, Input.mouseY, x, y) + 90;


    var distance:Number = FP.distance(x, y, Input.mouseX, Input.mouseY);

    if (distance > lastDistance) {moveDir = -1};

    if (distance < lastDistance) {moveDir = 1};


    if (moveKeyPressed == true && moveDir == 1) {"walk", false, 0)}

    else {"stand", false, 0) }

    lastDistance = distance



I tried using a variable to determine if one of my movement keys (WASD) was pressed and another variable to determine if the character was moving toward or away from the mouse. I then used conditionals to determine whether to play the forward walking animation. For some reason, that isn’t working.Just to be clear, I don’t have a backward walking animation yet, not that that’s important.