3
\$\begingroup\$

Is it possible to have a diagonal movement animation in monogame/xna?

As far as I can tell it's not possible to recognise multiple keypresses (ie "A && W" or "S && D") as unique events - everything I've seen regarding diagonal movement basically goes;

if(W)
{
    player.position.x = player.position.x + 1
}
if(S)
{
    player.position.x = player.position.x - 1
}
if(A)
{
    player.position.y = player.position.y - 1
}
if(D)
{
    player.position.y = player.position.y + 1
}

And that obviously works in most instances, my problem is I have specific diagonal movement animations that I need to display, my characters are people and I can't just rotate them like a ship or a car - and as far as I can tell....

if(W && A)
{
    player.position.x = player.position.x + 1
    player.position.y = player.position.y - 1
}

Isn't possible?

The only way I can think to accomplish what I need is to detect diagonal movement after the input handling - ie if both x AND y positions are different or are going to be different to the previous position, set the relevant diagonal to true and show the relevant animation.

Is there a 'correct' or easier way to do this?

\$\endgroup\$

1 Answer 1

4
\$\begingroup\$

Instead of directly modifying the position of the player you could add another Vector2 variable named, for example, Direction. When a player pressed W you set direction to (0,1) and if it was up & left then set it to (-1,1) and you could then apply animation according to the direction you're going.

And then to move it you should calculate a new position of the player using the direction and speed.

Wether or not this is the "correct" way I leave up to you - it's one solution at least.

EDIT: It should be noted that a usual direction vector is normalized so it wouldn't be (-1,1) I just figured it might be easier to use full integers for the animation part of it..

\$\endgroup\$
1
  • \$\begingroup\$ Yeah, it seems acting on a variable post hoc would seem to be the way to go. \$\endgroup\$ Aug 30, 2016 at 16:54

You must log in to answer this question.

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