r/gamemaker • • 11d ago

Help! Just getting started, how do I make switching sprites work with different amounts of frames?

I'm following a tutorial and it seemed pretty simple, sprite_index was fairly intuitive, and in the video it worked eventhough the sprites had different amounts of frames, but for me, I have one that's 4 frames as a placeholder idle and an 8 frame placeholder walk, I was just messing around so it ended a bit longer, but the issue is that during the walk it only goes to frame 4/8 before looping. The tutorial didn't address that because it didn't happen, and I thought I followed how I should but evidently I didn't.

1 Upvotes

9 comments sorted by

1

u/Kitchen_Builder_9779 Professional if statement spammer 11d ago

Share the code maybe?

1

u/Privatizitaet 11d ago
sprite_index = sprPlayerIdle;
if (keyboard_check(vk_left)){
x -= 4
image_xscale = 1;
sprite_index = sprPlayerWalk;

}
if (keyboard_check(vk_right)){
x += 4
image_xscale = -1;
sprite_index = sprPlayerWalk;

}

This is what I have, it's very simple.

0

u/Kitchen_Builder_9779 Professional if statement spammer 11d ago

Why are you setting the sprite to Idle before even checking for movement?
Use else and else if statements maybe?

1

u/Privatizitaet 11d ago

So it resets to the idle when I let go of the button regardless of which button I press, but I can try with else

1

u/Kitchen_Builder_9779 Professional if statement spammer 11d ago

Have you changed your code to include else statements like I suggested?
If so can I see the changed code?

1

u/Privatizitaet 10d ago

Yeah, that seems to work now, thanks, although it doesn't really make sense to me why.

if (keyboard_check(vk_left)){
x -= 4
image_xscale = 1;
sprite_index = sprPlayerWalk;

} else if (keyboard_check(vk_right)){
x += 4
image_xscale = -1;
sprite_index = sprPlayerWalk;

} else {
sprite_index = sprPlayerIdle;
}

3

u/Kitchen_Builder_9779 Professional if statement spammer 10d ago

Probably because your old code switched your sprite to the idle EVERY FRAME, instead of only when you were not moving.
This was not visible, cause it would instantly be switched back, but it probably messed something up with the image indexes

1

u/germxxx 10d ago

To be more specific, as you change the sprite_index, your image_index stays the same.

But since your idle sprite only has 4 frames, the image_index (current frame of the sprite to draw) is higher (eventually) than possible, and is then set back to 0 again (first frame).

So for example, changing the sprite of an object to a sprite with a single frame, will always reset the image_index. It will only carry over if its a valid frame index for the new sprite.

1

u/KitsuneFaroe 9d ago

Changing built-in instance variables doesn't just change that variable, it also changes other variables that naturally depend on it. Like how changing speed also changes hspeed and vspeed, or how changing hspeed may also change the direction of movement. The same happens when you change sprite_index, it warps image_index to not be beyond the number of frames of the sprite.