• Hey! Guest! The 36th GMC Jam will take place between February 27th, 12:00 UTC - March 2nd, 12:00 UTC. Why not join in! Click here to find out more!

Legacy GM [Resolved] Sprite wont start from the first frame

Z

Zakarul

Guest
I have a sprite that wont' start from the beginning. It usually start from frame 30 to 0,1,2 etc.... I've already tried to add image_index = 0 but doing this just freeze the sprite at 0. Maybe i am missing some knowledge about image_index.
Are there any way to fix this?

every sprites are called from the script "scr_player_sprites"
this is the code

Code:
if state == scr_player_pullup
{
if !place_empty(x+1,y) && place_empty(x-1,y)
    {
    sprite_index = spr_pullup;
    mask_index = Spr_player_256;
    image_xscale = 1;
    image_speed = 1.5;
    }
else
if !place_empty(x-1,y) && place_empty(x+1,y)
    {
    sprite_index = spr_pullup;
    mask_index = Spr_player_256;
    image_xscale = -1;
    image_speed = 1.5;
    }
}
i am getting mad for this

Gamemaker Studio 1.4
 

Perseus

Not Medusa
Forum Staff
Moderator
This is desired behaviour. Changing the sprite does not change the index of the currently visible sub-image, so if you change the sprite on sub-image number 3, the new sprite will be drawn with that sub-image visible (assuming it has the same number of sub-images).

Setting image_index to 0 that way won't help, for it would keep on getting reset to 0 every step. Consequently, the sprite would be stuck at the first sub-image. You can use a variable instead, which would hold the last sprite index. And in case it is not the same as the current sprite index, set image_index to 0 and the variable to the new sprite index. This ensures that the image index gets set to 0 only once per sprite swap.

Code:
if (last_sprite != sprite_index) {
   image_index = 0;
   last_sprite = sprite_index;
}
 
Z

Zakarul

Guest
This is desired behaviour. Changing the sprite does not change the index of the currently visible sub-image, so if you change the sprite on sub-image number 3, the new sprite will be drawn with that sub-image visible (assuming it has the same number of sub-images).

Setting image_index to 0 that way won't help, for it would keep on getting reset to 0 every step. Consequently, the sprite would be stuck at the first sub-image. You can use a variable instead, which would hold the last sprite index. And in case it is not the same as the current sprite index, set image_index to 0 and the variable to the new sprite index. This ensures that the image index gets set to 0 only once per sprite swap.

Code:
if (last_sprite != sprite_index) {
   image_index = 0;
   last_sprite = sprite_index;
}
OMG you saved me! thank you!

i placed the code in the end step and "last_sprite = sprite_index" in the create event!
now it works like a charm
 

Roderick

Member
As an alternate method, you could use image_index = 0, but, as Ragarnak said, you can't do it in a Step event. You would have to put it in the same spot where you change your state instead.
 
Yes. it. is! A very desired behavior. And you sir, are awesome. I've been looking everywhere for a solution to this and you just made my day. They need to add this code to the hints section of Game Maker or just build it into the dang system as a switch!


This is desired behaviour.

Code:
if (last_sprite != sprite_index) {
   image_index = 0;
   last_sprite = sprite_index;
}
 

PWL

Member
Instead of having a variable holding the last sprite, I'd rather check if the sprite is the same and do the sprite-change if it is not already the sprite you want to change to.

Then again it's a matter of preference, but I, myself is totally in love with small, handy scripts like this:

Code:
/// sprite_change(spr);

if(sprite_index != argument0){
    sprite_index = argument0;
    image_index = 0;
}
Code:
sprite_change(spr_pullup);
EDIT: Typos.
 
Top