• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

How do I change animated player sprite for few seconds?

M

Mindaugas

Guest
Hello fellow programers. I'm a newbie, so this hopefully won't be a difficult one. Ok, so I'm going through Shawn Spaldings tutorial about ''powerups''. He uses a simple block which changes color and jumps higher for 5secs with this code in obj_powerup colision with the player event:

jumpspeed = jumpspeed_powerup;
sprite_index = spr_player_powerup;
alarm [0] = 300;

(and later changes back the sprite to ''spr_player'' in player's alarm0 event)

The problem is that I'm not using 2 simple one image different color block sprites. I have 4 different sprites which are:
spr_player_idle;
spr_player_running;
spr_player_idle_powerup;
spr_player_running_powerup.

idle ones consists of 18 images and the running ones consists of 8 images (idk if that matters)

my sprites are animated by Shawn's ''animation'' tutorial, with a code in player's step event which is:

if (move !=0) image_xscale = move;
if (place_meeting (x, y+1, obj_wall))
{{
if (move !=0) sprite_index = spr_player_running;
else sprite_index = spr_player_idle;
}
image_speed = 0.2}

(i thought that copying this into powerup's colision event with sprite names changed will be enough, unfortunately it wasn't)
 

YoSniper

Member
One thing I like to recommend to people: Finite State Machines are your friend.

They make your code a lot more readable, and convenient to you later.

Here's my plan.

In your Create Event
Code:
state = "IDLE";
has_powerup = false;
In your Step Event
Code:
switch(state) {
    case "IDLE":
        if has_powerup {
            sprite_index = spr_player_idle_powerup;
        } else {
            sprite_index = spr_player_idle;
        }
        break;
    
    case "RUNNING":
        if has_powerup {
            sprite_index = spr_player_running_powerup;
        } else {
            sprite_index = spr_player_running;
        }
        break;
    
    //You have room for other states as needed
}
Before the switch/case block, it would help to write the code that determines what value "state" is so that you get the right sprite selection. I don't know exactly what conditions you are using to determine variables like "move" (or likewise hspeed and vspeed) so I left that part out.

My point is that if you organize your code like this, it can work wonders.

And don't forget the "break" statements. If you leave those out, code from earlier cases bleed into code for later cases, and that can cause issues.
 
M

Mindaugas

Guest
Thank you! I'll definitely try this when I'm back from work. If it makes makes any difference, since you wrote about my vspeed and hspeed, I use my own variables for speed. in create event:
Code:
hsp = 0;
vsp = 0;
movespeed = 4;
and in step event:
Code:
key_right = keyboard_check (vk_right);
key_left = -keyboard_check (vk_left);

move = key_left + key_right
hsp = move * movespeed;

x += hsp;
I think I'm not leaving anything out as far as the movement goes.
 

YoSniper

Member
Thank you! I'll definitely try this when I'm back from work. If it makes makes any difference, since you wrote about my vspeed and hspeed, I use my own variables for speed.
I like you. I do much the same thing in my own games, but when I talk to people on the forums, I tend to reference the built-in variables.

But yeah, with your code as shown above, you could have code like the following to determine your state:
Code:
if move == 0 {
    state = "IDLE";
} else {
    state = "RUNNING";
}
I also don't know if you have separate sprites for jumping, but you could add in a state for jumping/falling as well, and use your vsp variable as a check for that (or simply check to see if the player is standing on a solid surface instead.)

All in all, it certainly looks like you're well on your way.
 
C

CoderJoe

Guest
Didn't read through everything but yeah state machines are helpful. I usually just use a couple simple variables rather than a full state machine. If the player is moving, then I set the image_speed to 0.5 (or something like that) to animate it. When the player stops, the image_speed is set to 0. I then also check what the player is doing (running, jumping, etc...) and change the sprite_index accordingly. One last thing I do is check which way the player is going then set the image_xscale either to 1 or -1 so that the image is flipped the right way.
 
Top