Animation not

I tried Shaun Spalding's way but it did not want to cooperate with me, making my sprite disappear every time I walked. My sprite doesn't disappear now, but all it does is show the jump animation. I need it to play the correct animations with the correct actions and not disappear when I try to walk.

GML:
if move != 0 image_xscale = sign(move);
if on_ground and vsp = 0 { sprite_index = s_player_idle; }
if on_ground and (hsp != 0) { sprite_index = s_player_walk;}
if not on_ground { sprite_index = s_player_jump; }
if not on_ground and key_jump { sprite_index = s_player_flip }
 

Slyddar

Member
I haven't seen Shaun's code, but this seems wrong
Code:
if on_ground and vsp = 0 { sprite_index = s_player_idle; }
and you are checking similar things multiple times, which could definitely be improved on.

Anyway, the line should be referring to hsp, so should probably be :
Code:
if on_ground and hsp = 0 { sprite_index = s_player_idle; }
Your sprite maybe disappearing if image_xscale is being set to 0. You could try this instead :
Code:
if hsp != 0 image_xscale = sign(hsp);
 

Yal

šŸ§ *penguin noises*
GMC Elder
Try using the else keyword to make sure only one sprite change can happen. Also add some more parantheses to make sure composite statements are run in the right order. Finally, you should use == for comparisons, not =.
GML:
if (on_ground and (hsp == 0)) { sprite_index = s_player_idle; }
else if (on_ground and (hsp != 0)) { sprite_index = s_player_walk;}
else if (not on_ground) { sprite_index = s_player_jump; }
else if ((not on_ground) and key_jump) { sprite_index = s_player_flip }
 

Slyddar

Member
else if (not on_ground) { sprite_index = s_player_jump; }
else if ((not on_ground) and key_jump) { sprite_index = s_player_flip }
This won't work. If !on_ground, the 2nd condition will never trigger.

He really should have it like this :
Code:
if on_ground {
  //on ground checks
  if hsp == 0 sprite_index = s_player_idle else sprite_index = s_player_walk;
} else {
  //not on ground checks
  if key_jump sprite_index = s_player_flip else sprite_index = s_player_jump;
}
 
I will try your suggestions. This is how I currently have the code, which is more or less how Shaun had it, or at least it has the same effect: sprite disappearing when cycling to the walk animation. It sometimes flickers to wrong parts of the screen where I can see the animation play and when it does the animation does play correctly.

GML:
if move != 0 image_xscale = move;
if (place_meeting(x, y+1, o_solid)) {
    if (hsp==0) {
        sprite_index = s_player_idle; }
    if (hsp != 0) {
        sprite_index = s_player_walk; }
}
if not (place_meeting(x, y+1, o_solid)) {
    if (vsp < 0) {
        sprite_index = s_player_jump; }
    if (vsp > 0) {
        sprite_index = s_player_flip }      
}
 
Last edited:
This won't work. If !on_ground, the 2nd condition will never trigger.

He really should have it like this :
Code:
if on_ground {
  //on ground checks
  if hsp == 0 sprite_index = s_player_idle else sprite_index = s_player_walk;
} else {
  //not on ground checks
  if key_jump sprite_index = s_player_flip else sprite_index = s_player_jump;
}
I tried this but the sprite never left the falling state.
 
Try using the else keyword to make sure only one sprite change can happen. Also add some more parantheses to make sure composite statements are run in the right order. Finally, you should use == for comparisons, not =.
GML:
if (on_ground and (hsp == 0)) { sprite_index = s_player_idle; }
else if (on_ground and (hsp != 0)) { sprite_index = s_player_walk;}
else if (not on_ground) { sprite_index = s_player_jump; }
else if ((not on_ground) and key_jump) { sprite_index = s_player_flip }
I also tried this way but the sprite never left the falling state.
 
I'll go ahead and post my entire code for the player.

Create
GML:
//Gravity
grav = 0.2;

//Horizontal & Vertical Speeds
hsp = 0;
hsp_carry = 0;
vsp = 0;

//On Ground
on_ground = place_meeting(x, y + 1, o_wall);

//Jumps Speed & Move Speed
jspeed_normal = 6;
jspeed_powered = 14;
mspeed = 4;

jumps = 0;
jumpsmax = 2

jspeed = jspeed_normal;

if (global.checkpointR == room) {
    x = global.checkpointx;
    y = global.checkpointy;
}

key_down = 0;

gamepad_set_axis_deadzone(0, 0.7);
Step
GML:
key_right = keyboard_check(ord("D")) || (gamepad_axis_value(0, gp_axislh) > 0);
key_left = keyboard_check(ord("A")) || (gamepad_axis_value(0, gp_axislh) < 0);
key_jump = keyboard_check_pressed(vk_numpad0) || keyboard_check_pressed(vk_space) || gamepad_button_check_pressed(0, gp_face1);
key_down = keyboard_check(ord("S")) || (gamepad_axis_value(0, gp_axislv) > 0);

#region React to Inputs
//React to inputs
var move = key_right - key_left;
hsp = move * mspeed;

if (vsp < 10) vsp += grav;

if (place_meeting(x, y + 1, o_wall)) {
    jumps = jumpsmax;
}


if key_jump and jumps > 0 {
    jumps -= 1;
    vsp = - jspeed;
}

/*
//Wall Jump
if (key_jump) and (place_meeting(x+1,y,o_wall)) || place_meeting(x-1,y,o_wall) {
    jumps = 1;
}
*/


#endregion

#region Collisions
var hsp_final = hsp + hsp_carry;
hsp_carry = 0;

//Horizontal Collision
if (place_meeting(x + hsp_final, y, o_wall)) {
    while (!place_meeting(x+sign(hsp_final), y, o_wall)) {
        x += sign(hsp_final);
    }
    hsp_final = 0;
    hsp = 0;
}

x += hsp_final;

//Vertical Collision
if (place_meeting(x, y + vsp, o_wall)) {
    while not (place_meeting(x, y + sign(vsp), o_wall)) {
        y += sign(vsp);
    }
    vsp = 0;
}

y += vsp;
#endregion

#region Animation

if move != 0 image_xscale = move;
if (place_meeting(x, y+1, o_solid)) {
    if (hsp==0) {
        sprite_index = s_player_idle; }
    if (hsp != 0) {
        sprite_index = s_player_walk; }
}
if not (place_meeting(x, y+1, o_solid)) {
    if (vsp < 0) {
        sprite_index = s_player_jump; }
    if (vsp > 0) {
        sprite_index = s_player_flip }      
}

/*if move != 0 image_xscale  = move;
if (place_meeting(x, y + 1, o_wall)) {
    if (move !=0) sprite_index = s_player_walk; else sprite_index = s_player_idle; }
else {
    if (vsp < 0) sprite_index = s_player_jump; else sprite_index = s_player_flip; }
*/
#endregion
Alarm
GML:
jspeed = jspeed_normal;
sprite_index = s_player_flip;
 

Slyddar

Member
if (hsp==0) { sprite_index = s_player_idle; } if (hsp != 0) { sprite_index = s_player_walk; }
The point of what we are saying is can you see these 2 options are mutually exclusive. There is no need to ask if hsp != 0, as your first if checks if it is equal to zero, and if that's not true, well there is no need to ask if it's not equal to zero, as that must be true.
Mutually exclusive code like that can be set up like this :
Code:
if (hsp==0) sprite_index = s_player_idle; else sprite_index = s_player_walk;
 
The point of what we are saying is can you see these 2 options are mutually exclusive. There is no need to ask if hsp != 0, as your first if checks if it is equal to zero, and if that's not true, well there is no need to ask if it's not equal to zero, as that must be true.
Mutually exclusive code like that can be set up like this :
Code:
if (hsp==0) sprite_index = s_player_idle; else sprite_index = s_player_walk;
As per your advice, I did change it to the "else" statement but the effect remains the same.

Link to my file if you want to take a look at it.
 

Nidoking

Member
on_ground = place_meeting(x, y + 1, o_wall);
You have this in the Create event. Great, it will see whether the instance is on the ground one time only. When did you plan to update this value? Then again, you're not using it in the latest code you posted, so why do you have it at all?
 
You have this in the Create event. Great, it will see whether the instance is on the ground one time only. When did you plan to update this value? Then again, you're not using it in the latest code you posted, so why do you have it at all?
I didn't use it in the latest build because it seemed like the problem was made worse with it.
 
You have this in the Create event. Great, it will see whether the instance is on the ground one time only. When did you plan to update this value? Then again, you're not using it in the latest code you posted, so why do you have it at all?
It only does the falling animation when I include on ground, but when I take it away it shows the idle animation when on ground.
 

Nidoking

Member
That means that your logic is faulty. If it's showing the idle animation, that means you told it to show the idle animation, whether that's what you intended or not. What you need to do, in the case of ANY problem, is to look at what you're writing and actually attempt to understand it. Ask yourself "Why is this showing the idle animation? What are the conditions that cause the game to display the idle animation here? If some of those conditions are not what should be happening, why are they wrong? If all of the conditions are correct, but should not actually lead to the idle animation displaying, then what part of my logic do I need to change to make it show a different animation when those conditions are true?" This will lead you to a problem. Fix the problem. NEVER change something if you don't know WHY you're changing it and what you intend for the change to accomplish. If it doesn't accomplish what you want, change it back before doing anything else and start from the beginning.
 
Top