GMS 2 How to change sprites

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);
}
 
M

maratae

Guest
layer_sprite_change? what version are you using?
What code are you using to make the player move?
 

Perseus

Not Medusa
Forum Staff
Moderator
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. :)
 
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. :)

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)
 
Top