GML Sprite animation canceling out other animations(SOLVED)

A

Alexander Olofsson

Guest
So, just have a look at my code:

Code:
if(hsp != 0 && vsp = 0)
{
    sprite_index = spr_player_running;
    image_speed = 0.3;
} else {
    sprite_index = spr_player;
}

if (vsp < 0 && !place_meeting(x,y,par_solids))
{
    sprite_index = spr_jump;
} else {
    sprite_index = spr_falling;
}

That is in the STEP EVENT and the solution might be really simple, but I'm new to coding, and I can't really see why the problem occurs.. so, the problem is that the sprite_index equals spr_falling all the time, even when I am touching ground, and that if statement is not supposed to run if I'm not touching ground, and I really can't figure it out. I really need help.

I watched Shaun Spalding's platformer tutorial, in case that might be important.

Thanks in advance! :D
 

Simon Gust

Member
You need to rearrange the if statements a bit
Code:
if (!place_meeting(x,y+1,par_solids))
{
    if (vsp > 0)
    {
        sprite_index = spr_falling;
    }
    else
    {
        sprite_index = spr_jump;
    }   
}
else
{
    if (hsp != 0)
    {
        sprite_index = spr_player_running;
        image_speed = 0.3;
    }
    else
    {
        sprite_index = spr_player;
    }
}
 

Paskaler

Member
If you want to check if you're touching the ground with place_meeting you have to add 1 to y.
Here's a rewritten version of the code above. It's structured a tad bit different, but the only real difference is the call to place_meeting using y + 1 instead of just y.
Code:
var on_ground = place_meeting(x, y + 1, par_solids);
if on_ground {
    if hsp != 0 {
        sprite_index = spr_player_running;
        image_speed = 0.3;
    } else {
        sprite_index = spr_player;
    }
} else if vsp < 0 {
    sprite_index = spr_jump;
} else {
    sprite_index = spr_falling;
}
 
A

Alexander Olofsson

Guest
Thank you so much bro :DD
I'll keep that in mind! :)
 
Top