SOLVED I need help with animation for two sprites

Nocturne

Friendly Tyrant
Forum Staff
Admin
Okay, first thing's first... PLEASE try to post any code using the [CODE=gml][/CODE] tags rather than screenshots! Makes editing and reading a lot easier for everyone... The second thing is to say that I would suggest "compartmentalising" things. By this, what I mean is break everything down into sections and work on each section as an individual unit. For example, at the moment, you have it so that the player moves instantly when the key is pressed... This is not normally the best idea! What you should do is get the player input and store that in variables, then deal with the movement in a separate section. This will make things (like changing sprite) a lot easier going forward. So:

GML:
// CREATE EVENT
spd = 2; //This controls the speed of movement

// STEP EVENT
var _hspd = 0; // temp vars to store horizontal and vertical speed
var _vspd = 0;

// Deal with input first
if keyboard_check(vk_left) _hspd = -1;
if keyboard_check(vk_right) _hspd = 1;
if keyboard_check(vk_up) _vspd = -1;
if keyboard_check(vk_down) _vspd = 1;

// Now deal with movement
if _hspd != 0
{
x += spd * _hspd;
}

if _vspd != 0
{
y += spd * _vspd;
}
Now that you've separated these things out you can add in a further section to deal with the sprite (again, a separate section for dealing with the sprites and animations):

GML:
// STEP EVENT, after everything else
// Deal with animation
if _vspd != 0 || _hspd != 0
{
sprite_index = spr_Player_Running;
}
else sprite_index = spr_Player_Idle;
By splitting your code up like this into logical "blocks" or "compartments", you are making it easier to visualise what is actually happening, and also making it a LOT easier to change and edit things as you go along.

Hope that helps!

:)
 

Ommn

Member
in step event:
before your codes
put this code:
GML:
var _x=x;
and after your codes
put this code:
GML:
if x==_x{sprite_index=spr_stop;}else{sprite_index=spr_walk;}
 

607

Member
Okay, first thing's first... PLEASE try to post any code using the [CODE=gml][/CODE] tags rather than screenshots! Makes editing and reading a lot easier for everyone... The second thing is to say that I would suggest "compartmentalising" things. By this, what I mean is break everything down into sections and work on each section as an individual unit. For example, at the moment, you have it so that the player moves instantly when the key is pressed... This is not normally the best idea! What you should do is get the player input and store that in variables, then deal with the movement in a separate section. This will make things (like changing sprite) a lot easier going forward. So:

GML:
// CREATE EVENT
spd = 2; //This controls the speed of movement

// STEP EVENT
var _hspd = 0; // temp vars to store horizontal and vertical speed
var _vspd = 0;

// Deal with input first
if keyboard_check(vk_left) _hspd = -1;
if keyboard_check(vk_right) _hspd = 1;
if keyboard_check(vk_up) _vspd = -1;
if keyboard_check(vk_down) _vspd = 1;

// Now deal with movement
if _hspd != 0
{
x += spd * _hspd;
}

if _vspd != 0
{
y += spd * _vspd;
}
Now that you've separated these things out you can add in a further section to deal with the sprite (again, a separate section for dealing with the sprites and animations):

GML:
// STEP EVENT, after everything else
// Deal with animation
if _vspd != 0 || _hspd != 0
{
sprite_index = spr_Player_Running;
}
else sprite_index = spr_Player_Idle;
By splitting your code up like this into logical "blocks" or "compartments", you are making it easier to visualise what is actually happening, and also making it a LOT easier to change and edit things as you go along.

Hope that helps!

:)
This does have a different result than the code in the OP. With OP's code, when you press both up and down at the same time, the character does not move. With your code, when you do that, the character moves down. Of course your recommendation of compartmentalisation is still good, and if OP's behaviour is the intended one
Code:
_hspd = -1;
could simply be changed to
Code:
_hspd += -1;
and so forth.

Edit: Using the built-in variables hspeed and vspeed would also work, right?
 
Last edited:
Top