GM:S 1.4 [Resolved] Sprite wont start from the first frame

Discussion in 'Programming' started by Zakarul, Feb 8, 2017.

  1. Zakarul

    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
     
  2. Perseus

    Perseus Not Ragarnak Forum Staff Moderator

    Joined:
    Dec 29, 2016
    Posts:
    953
    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;
    }
    
     
  3. Zakarul

    Zakarul Guest

    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
     
  4. Roderick

    Roderick Member

    Joined:
    Jun 22, 2016
    Posts:
    580
    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.
     
  5. General Woodchuck

    General Woodchuck Member

    Joined:
    Jan 25, 2018
    Posts:
    19
    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!


     
  6. PWL

    PWL Member

    Joined:
    Jun 21, 2016
    Posts:
    28
    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.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice