Animated Sprite Doesn't Work!

I

its_Johnny

Guest
hello everyone,
I am facing an issue on GameMaker Studio 2, regarding a sprite animation.
I have done several sprites for the hero (the character the player can use), one if the hero object dent move, one if for when the character uses the stairs. I have a problem with this exact sprite, the other 2 work fine and do the animation but not this one. I'm sorry if i'm not clear enough but I'm pretty new at coding. thanks for your help, here's the code I've written
Code:
//stairs meeting
if (key_up || key_down)
{
    if(place_meeting(x,y,o_stairs)) ladder = true;
}
if ladder = true
{
    grv = 0;
    if (key_up) vsp = -2;
    if (key_down) vsp = +2;
    if!place_meeting(x,y,o_stairs) ladder = false;
    if (key_jump) ladder = false;
}
if ladder = false
{
    grv = 0.25;
}

//Animation
if (keyboard_check(vk_left))
{
    image_xscale = -0.1
}
if (keyboard_check(vk_right))
{
    image_xscale = 0.1
}
if (hsp < 0)
{
    sprite_index = S_player_run
}
if (hsp > 0)
{
    sprite_index = S_player_run
}
if (hsp = 0)
{
    sprite_index = S_player
}
if (place_meeting(x,y,o_stairs) && keyboard_check(vk_up))
{
    sprite_index = S_Player_stairs;
    image_speed = true;
}
if (place_meeting(x,y,o_stairs) && keyboard_check(vk_down))
{
    sprite_index = S_Player_stairs;
    image_speed = true;
}
 
D

DigitalClark

Guest
if (place_meeting(x,y,o_stairs) && keyboard_check(vk_up)) { sprite_index = S_Player_stairs; image_speed = true; } if (place_meeting(x,y,o_stairs) && keyboard_check(vk_down)) { sprite_index = S_Player_stairs; image_speed = true; }
As stated above, image_speed isn't a boolean (true or false).
Instead you have to set it to a value, meaning 0 is completely still, 1 is normal speed, and -1 is reversed normal speed.
You could do something like this:

Code:
if (place_meeting(x,y,o_stairs) && keyboard_check(vk_up))
{
    sprite_index = S_Player_stairs;
    image_speed = 1;
} else {
    image_speed = 0;
};

if (place_meeting(x,y,o_stairs) && keyboard_check(vk_down))
{
    sprite_index = S_Player_stairs;
    image_speed = 1;
} else {
    image_speed = 0;
};
 
I

its_Johnny

Guest
As stated above, image_speed isn't a boolean (true or false).
Instead you have to set it to a value, meaning 0 is completely still, 1 is normal speed, and -1 is reversed normal speed.
You could do something like this:

Code:
if (place_meeting(x,y,o_stairs) && keyboard_check(vk_up))
{
    sprite_index = S_Player_stairs;
    image_speed = 1;
} else {
    image_speed = 0;
};

if (place_meeting(x,y,o_stairs) && keyboard_check(vk_down))
{
    sprite_index = S_Player_stairs;
    image_speed = 1;
} else {
    image_speed = 0;
};
thanks so so so much for your help, Ive tried to change the values to image_speed = 1, 0 and -1 etc but nothing changes, the sprite do changes but the animation still doesn't start :(
 
D

DigitalClark

Guest
I might know what the issue could be (to be sure tho, I would have to see your whole source code).
I assume you have the code in the step event, which is correct, but this way everytime you press and hold the up or down key, the sprite_index gets set to S_Player_stairs every game step, which essentially resets the sprite_index to it's first frame. Try this instead:
Code:
if (place_meeting(x,y,o_stairs) && keyboard_check_pressed(vk_up))
{
    sprite_index = S_Player_stairs;
    image_speed = 1;
};

if (place_meeting(x,y,o_stairs) && keyboard_check_pressed(vk_down))
{
    sprite_index = S_Player_stairs;
    image_speed = 1;
};
This only changes the sprite_index once, when you first press the key(s) and the stairs are detected.
 
I

its_Johnny

Guest
it still doesn't work, as you said all the code is in the step event, if you want I can show you my entire code but I think I had better to simply cut the stairs idea out of the project...

Code:
//get player imput
key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump = keyboard_check_pressed(vk_space);
key_up = keyboard_check_pressed(vk_up);
key_down = keyboard_check_pressed(vk_down);

//calculate movemet
var move = key_right - key_left;

hsp = move * walksp;

vsp = vsp + grv;

if (place_meeting(x,y+1,O_wall)) && (key_jump)
{
    vsp = -10;
}

//horizontal collision
if (place_meeting(x+hsp,y,O_wall))
{
    while (!place_meeting(x+sign(hsp),y,O_wall))
    {
        x = x + sign(hsp);
    }
    hsp = 0;
}
x = x + hsp;

//vertical collision
if (place_meeting(x,y+vsp,O_wall))
{
    while (!place_meeting(x,y+sign(vsp),O_wall))
    {
        y = y + sign(vsp);
    }
    vsp = 0;
}
y = y + vsp;

//stairs meeting
if (key_up || key_down)
{
    if(place_meeting(x,y,o_stairs)) ladder = true;
}
if ladder = true
{
    grv = 0;
    if (key_up) vsp = -2;
    if (key_down) vsp = +2;
    if!place_meeting(x,y,o_stairs) ladder = false;
    if (key_jump) ladder = false;
}
if ladder = false
{
    grv = 0.25;
}

//Animation
if (keyboard_check(vk_left))
{
    image_xscale = -0.1
}
if (keyboard_check(vk_right))
{
    image_xscale = 0.1
}
if (hsp < 0)
{
    sprite_index = S_player_run
}
if (hsp > 0)
{
    sprite_index = S_player_run
}
if (hsp = 0)
{
    sprite_index = S_player
}
if (place_meeting(x,y,o_stairs) && keyboard_check_pressed(vk_up))
{
    sprite_index = S_Player_stairs;
    image_speed = 1;
};

if (place_meeting(x,y,o_stairs) && keyboard_check_pressed(vk_down))
{
    sprite_index = S_Player_stairs;
    image_speed = 1;
};
 
D

DigitalClark

Guest
The sprite_index changes, but the animation still won't play, right?

I'm not completely sure (actually only a guess too), but you could try:
Code:
if (place_meeting(x,y,o_stairs)
{
if ( ( keyboard_check_pressed(vk_up) ) || ( keyboard_check_pressed(vk_down) ) )
{
    sprite_index = S_Player_stairs;
    image_speed = 1;
};
};
 

Bentley

Member
hello everyone,
I am facing an issue on GameMaker Studio 2, regarding a sprite animation.
I have done several sprites for the hero (the character the player can use), one if the hero object dent move, one if for when the character uses the stairs. I have a problem with this exact sprite, the other 2 work fine and do the animation but not this one. I'm sorry if i'm not clear enough but I'm pretty new at coding. thanks for your help, here's the code I've written
Code:
//stairs meeting
if (key_up || key_down)
{
    if(place_meeting(x,y,o_stairs)) ladder = true;
}
if ladder = true
{
    grv = 0;
    if (key_up) vsp = -2;
    if (key_down) vsp = +2;
    if!place_meeting(x,y,o_stairs) ladder = false;
    if (key_jump) ladder = false;
}
if ladder = false
{
    grv = 0.25;
}

//Animation
if (keyboard_check(vk_left))
{
    image_xscale = -0.1
}
if (keyboard_check(vk_right))
{
    image_xscale = 0.1
}
if (hsp < 0)
{
    sprite_index = S_player_run
}
if (hsp > 0)
{
    sprite_index = S_player_run
}
if (hsp = 0)
{
    sprite_index = S_player
}
if (place_meeting(x,y,o_stairs) && keyboard_check(vk_up))
{
    sprite_index = S_Player_stairs;
    image_speed = true;
}
if (place_meeting(x,y,o_stairs) && keyboard_check(vk_down))
{
    sprite_index = S_Player_stairs;
    image_speed = true;
}
Just a heads up:

When you are setting the image_xscale, do you want the player to face left and right, or do you want to shrink the player's bounding box?

Because if it's the former, you would use image_xscale = -1 and image_xscale = 1, not image_xscale = -0.1. And If so, make sure the origin is centered. Otherwise, when you flip image_xscale, part of the sprite could overlap with a wall.
 
Last edited:

TheouAegis

Member
Remove image_speed from all code, first. You have an idle sprite, so there is likely no need for you to mess with image_speed at all.

Second, make sure all your sprites have a speed set in them. In GMS2 you set the speeds of sprites in the sprite itself.
 
I

its_Johnny

Guest
Hey everyone! thanks so so so much for your answer, to see what was going on I changed these lines
Code:
if (place_meeting(x,y,o_stairs) && keyboard_check_pressed(vk_up))
{
    sprite_index = S_Player_stairs;
    image_speed = 1;
};
to these:
Code:
if (keyboard_check(vk_up))
{
sprite_index = S_Player_stairs
}
in this way, in any moment by pressing vk_up the sprite should animate, right? (S_Player_stairs is an animated sprite); but its what happened next that I found almost disturbing: by pressing only vk_up the sprite does change from the standard one (S_Player) to S_player_stairs but without moving; but -here I have to admit I start questioning about paranormality- if I press at the same time vk_up and vk_right or vk_left the sprite does its animation. it looks like it only animates when hsp !=0 but I've never wrote such line in my code, as you can see few messages before. any idea why? thanks so so so much again for your time! :D
The sprite_index changes, but the animation still won't play, right?

I'm not completely sure (actually only a guess too), but you could try:
Code:
if (place_meeting(x,y,o_stairs)
{
if ( ( keyboard_check_pressed(vk_up) ) || ( keyboard_check_pressed(vk_down) ) )
{
    sprite_index = S_Player_stairs;
    image_speed = 1;
};
};
 
I

its_Johnny

Guest
btw I've tried all the solutions you guys wrote me but none worked. but I really appreciate it, thanks again!
 

TheouAegis

Member
First test, is the player object the very first object in your resource tree?If so, move it down the resource tree so that it is no longer the first object. Then test your game. If this does resolve the issue, then your issue is likely you are using dot notation or with() targeting a variable that was not properly set to an instance id or noone.

Check every event in the player object. I'm assuming your player object is not a child of another object, but if it is, check the parent object. if you are able to completely rule out any more code in the player object, then go through every other object in the game. Especially check any terrain object such as walls or stairs, make sure they do not have any code that affects the player. In the case of any enemy objects that may be in the room, make sure if they have code which sets image speed based on hsp that is pointing to themselves directly (i.e., no dot notation).
 

Relic

Member
I’m surprised the sprite is changing to the stair sprite.

Each step you check what the hsp speed is and set the sprite to idle or moving, which is fine. You then check if on the stair and PRESSED up to change to the stair sprite. Keyboard_check_pressed is only true on the step the key was pushed down. You should have used keyboard_check() which is true every step while they key remains held down.
 
Top