• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!
  • Hello [name]! Thanks for joining the GMC. Before making any posts in the Tech Support forum, can we suggest you read the forum rules? These are simple guidelines that we ask you to follow so that you can get the best help possible for your issue.

Question - Code Player Animation Problems

A

Alex03

Guest
I am having problems when I move horizontally and vertically. Example: If I press the right key and the down key, the animation switches correctly; the problem is: the animation only shows the first 2 animations on loop. So if i'm trying to walk down + right, it looks like i'm limping that way. How do I fix this?
Here is the code-
///scr_move
scr_keys();
// Move Right
if (rightkey) {
phy_position_x += spd;
sprite_index = spr_player_right;
image_speed = 1;
}
// Move left
if (leftkey) {
phy_position_x -= spd;
sprite_index = spr_player_left;
image_speed = 1;
}
// Move up
if (upkey) {
phy_position_y -= spd;
sprite_index = spr_player_up;
image_speed = 1;
}
// Move down
if (downkey) {
phy_position_y += spd;
sprite_index = spr_player_down;
image_speed = 1;
}
// Idle
if (!rightkey && !leftkey && !upkey && !downkey) {
image_index = 0;
}

// Vertical and Horizontal Movement
if (rightkey || leftkey) && (downkey || upkey) {
spd = 3;
}
 
N

NeZvers

Guest
That might be because you have separate ifs and one can override other.
So make series "else if", but understand that first if in series that return "true" will bypass rest. That means if you have pressed diagonal direction but first in list is vertical check, then it will be the one and only one used.
 
A

Alex03

Guest
That might be because you have separate ifs and one can override other.
So make series "else if", but understand that first if in series that return "true" will bypass rest. That means if you have pressed diagonal direction but first in list is vertical check, then it will be the one and only one used.
I don't know if this is what you meant, but I updated it to this:
scr_keys();
if (upkey) {
phy_position_y -= spd;
sprite_index = spr_player_up;
image_speed = 1;
} else if (downkey) {
phy_position_y += spd;
sprite_index = spr_player_down;
image_speed = 1;
} else if (rightkey) {
phy_position_x += spd;
sprite_index = spr_player_right;
image_speed = 1;
} else if (leftkey) {
phy_position_x -= spd;
sprite_index = spr_player_left;
image_speed = 1;
} else if (!rightkey && !leftkey && !upkey && !downkey) {
image_index = 0;
} else if (rightkey || leftkey) && (downkey || upkey) {
spd = 3;
}

Now, the sprite only goes strictly left, right, up or down; without corner movement.
 
N

NeZvers

Guest
Well, you didn't put condition for diagonal movement.
By the way you also override spd value to 3 and essentially everywhere else it's called is 3 after that.
I'm on mobile so I hope I'll write some ideas right
Code:
var hinput = rightkey - leftkey;
var vinput = downkey - upkey;
var _spd = spd;
If (hinput != 0 && vinput != 0)
{
_spd = spd * 0.72; // diagonal movement
}
If (hinput != 0)
{
phy_position_x = _spd * hinput;
}
if (vinput != 0)
{
phy_position_y = _spd * vinput;
}
Thats regarding to movement.
You can work out sprites yourself.
 
Last edited:
Top