GMS 2 How to change sprites

Discussion in 'Programming' started by ItsThriller, Mar 19, 2017.

  1. ItsThriller

    ItsThriller Member

    Joined:
    Mar 19, 2017
    Posts:
    40
    My plan is to have my player object change to an animated walking sprite when it recognizes that I'm using W, A, S, or D. I thought I had it set up correctly, but apparently not. I'm new, so feel free to explain it to me like I'm a five year old.

    The "spr_player_standing_pistol" sprite is the standard sprite.
    The "spr_player_walking_pistol" sprite is the animated walking sprite is the animated one that I'm trying to switch to while walking.

    I can explain anything else that doesn't make sense in like five minutes time probably.

    Code:

    //Play Walking Sprite
    if (keyboard_check(ord("D"))) or (keyboard_check(ord("A"))) or (keyboard_check(ord("S"))) or (keyboard_check(ord("W")))
    {
    layer_sprite_change(spr_player_standing_pistol, spr_player_walking_pistol);
    }
     
  2. maratae

    maratae Member

    Joined:
    Mar 14, 2017
    Posts:
    405
    layer_sprite_change? what version are you using?
    What code are you using to make the player move?
     
  3. arirish

    arirish Member

    Joined:
    Mar 13, 2017
    Posts:
    266
    He's using GMS 2. It says it right there at the top of the post
     
    ItsThriller likes this.
  4. ItsThriller

    ItsThriller Member

    Joined:
    Mar 19, 2017
    Posts:
    40
    I'm using GMS2. I'm basically using the code from above to move just not all in one line, and without the layer sprite change thing
     
  5. Perseus

    Perseus Not Ragarnak Forum Staff Moderator

    Joined:
    Dec 29, 2016
    Posts:
    953
    You don't want layer_sprite_change, since it changes the sprite index assigned to a sprite element placed on an asset layer, not the sprite index assigned to the instance that runs the code. Read up on asset layers in the manual to see what they are. But you don't need them at all to achieve what you are trying to do here. ;)

    You need to use the sprite_index variable instead.

    Code:
    if (keyboard_check(ord("D")) or keyboard_check(ord("A")) or keyboard_check(ord("S"))) or keyboard_check(ord("W"))) {
        sprite_index = spr_player_walking_pistol;
    }
    else {
        sprite_index = spr_player_standing_pistol;
    }
    
    Hope that helps. :)
     
    sercan likes this.
  6. ItsThriller

    ItsThriller Member

    Joined:
    Mar 19, 2017
    Posts:
    40

    How would I use that if I ended up having multiple sprites id want to switch to? (ex a shooting sprite and walking with a shotgun)
     

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