Best way to implement walking animations?

Hudsonius

Member
So I already have my sprites and player movement (no need to talk about that) and I was wondering what the most accurate way to put walking animations into my game would be. My game is top-down so it requires 4 directional movement. The reason I would like this is because, in the game, the direction your character is facing is the direction your melee attack goes, so, it's really annoying if you don't have good implement on the animations (also it looks bad w/out good timing on walking). I tried setting when hspeed >/< 0 sprite is left/right and image speed = 1 (same for vspeed) and setting the image speed to 0 when the player is still. This works, but the player faces the wrong direction if two keys are pressed at once and you lift one (this happens a lot) and the player often has a walking animation when still. If anyone could help it would be great, a link would be fine too.
 

TheouAegis

Member
If the player isn't changing direction when two keys are pressed and one released, then there are issues with your handling of inputs or speeds. Personally, I like storing all inputs or directions in one variable and deciding what to do after that.

input = ((UP << 1 | DOWN) << 1 | LEFT) << 1 | RIGHT;

So when RIGHT is pushed, the value is 1. When LEFT and RIGHT are both pushed, the value is 3. When UP and LEFT are pushed, the value is 10.

Conflicting directions need to be addressed, so I might do something like:

if input & 3 == 3 {input -= 3;}
if input & 12 == 12 {input -= 12;}

Then for the sprite, i'd just set a variable to input if input is set.

if input {sprite_dir = input;}

Then use that to access an array of sprites to use.

if input != sprite_dir {sprite_index = walk_sprites[sprite_dir];}

When attacking, you'd do the same thing.

if ATTACK {sprite_index = attack_sprites[sprite_dir];}
 
Top