• 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!

Change sprites when moving left?

Hi everyone,

This is another bit of code I haven't been able to work out on my own, despite finding similar posts; I am a beginner! I am making a platformer where I am using left and right facing sprites for idle, running, jumping, etc. The code I am using for changing animation was acquired through a Shaun Spalding tutorial, though instead of using left and right facing sprites, he just flipped the right facing sprite.

My previous attempts only managed to have the running sprite change when the player's hsp was -hsp, but I couldn't work out what to code for the idle and jumping sprites.
When the player is moving left, I would like sPlayerA to change to sPlayerALeft; sPlayer to sPlayerLeft; sPlayerR to sPlayerRLeft.

Below is the current sprite changing/animation coding used:
------------------------------------------------------------------------------------------------------
//Normal Animation - changing sprite, graphics, etc
if (!place_meeting(x, y + 1, oWall))
{
//jumping
sprite_index = sPlayerA;
image_speed = 0;
if (sign(vsp) > 0) image_index = 1; else image_index = 0;
}
else
{
canjump = 8;
image_speed = 1;
if (hsp == 0)
{
//idle
sprite_index = sPlayer;
else
{
//Running
sprite_index = sPlayerR;
if (key_fast)
walksp = 4 else walksp = 2.5
}
}
------------------------------------------------------------------------------------------------------
Thanks everyone for your help!
//Edit// I'm not wanting to flip as my left and right facing sprites are slightly different :)
 
Last edited:
K

KiD_Rager

Guest
There are several ways you could accomplish this.

Firstly, you could insert snippets of code that change the sprite when speed changes. For instance in the IDLE code, if (hsp == 0) && ( sprite_index == sPlayerRRight) { sprite_index = sPlayerR; }. If the player is no longer moving and still has the running sprite in the right direction, it will change into the sprite right idle image. Do the same with another IF statement but for the left. Repeat this process for each. You can double this for jumping, or do one expanded AND OR condition.

GML:
//idle
if hsp == 0 && ((sprite_index == sPlayerRRight) || (sprite_index == sPlayerARight)) {
    sprite_index = sPlayerRight;
}

Another can involve your left and right keys (whatever they are, WASD or Arrow etc). Same procedure; if button is left pressed change to sprite left (jumping or running) and the same for the right.

GML:
//jumping/running
if LEFT PRESSED {
    sprite_index = sPlayerALeft; //same setup for running left
}

else if RIGHT PRESSED {
    sprite_index = sPlayerARight; //same setup for running right
}
This code would go into your jumping and running states. Then you can use the first code snippet for your idle.

Now if you wanna simplify your life, using image_xscale will be a little quicker. Whatever side your player faces (left or right), we'll call that image_xscale = 1;. I'll assume your sprite faces right regularly, so that is a value of 1 for image_scale. If you do image_xscale = -1;, you can flip the sprite across the vertical axis so it now faces left. Thus, a good code snippet would be this:

GML:
if RIGHT PRESSED { image_index = 1; } //Face sprite normal (right)
else { image_index = -1; } //Face sprite oppsite (left)
**Note: We can use the ELSE without ELSE IF because [I'm assuming] your character only ever faces two directions: left or right. If you're not facing right, then the only other possible direction is left. Hence 1 and -1.

The great thing about this is that you don't need to add it to each section (jumping, running, idle). Just add it once before or after your code and you'll be good. Hope this helps! :)
 
There are several ways you could accomplish this.

Firstly, you could insert snippets of code that change the sprite when speed changes. For instance in the IDLE code, if (hsp == 0) && ( sprite_index == sPlayerRRight) { sprite_index = sPlayerR; }. If the player is no longer moving and still has the running sprite in the right direction, it will change into the sprite right idle image. Do the same with another IF statement but for the left. Repeat this process for each. You can double this for jumping, or do one expanded AND OR condition.

GML:
//idle
if hsp == 0 && ((sprite_index == sPlayerRRight) || (sprite_index == sPlayerARight)) {
    sprite_index = sPlayerRight;
}

Another can involve your left and right keys (whatever they are, WASD or Arrow etc). Same procedure; if button is left pressed change to sprite left (jumping or running) and the same for the right.

GML:
//jumping/running
if LEFT PRESSED {
    sprite_index = sPlayerALeft; //same setup for running left
}

else if RIGHT PRESSED {
    sprite_index = sPlayerARight; //same setup for running right
}
This code would go into your jumping and running states. Then you can use the first code snippet for your idle.

Now if you wanna simplify your life, using image_xscale will be a little quicker. Whatever side your player faces (left or right), we'll call that image_xscale = 1;. I'll assume your sprite faces right regularly, so that is a value of 1 for image_scale. If you do image_xscale = -1;, you can flip the sprite across the vertical axis so it now faces left. Thus, a good code snippet would be this:

GML:
if RIGHT PRESSED { image_index = 1; } //Face sprite normal (right)
else { image_index = -1; } //Face sprite oppsite (left)
**Note: We can use the ELSE without ELSE IF because [I'm assuming] your character only ever faces two directions: left or right. If you're not facing right, then the only other possible direction is left. Hence 1 and -1.

The great thing about this is that you don't need to add it to each section (jumping, running, idle). Just add it once before or after your code and you'll be good. Hope this helps! :)
Thanks for your response! The left pressed/right pressed part makes the most sense to me, though I haven't separated running, idle and jumping as separate states. Would I be able to implement this code within the code I've provided above, or do I need to separate them into states first? Thanks
 
T

teamrocketboyz

Guest
are both the right and left sprites the same just facing the opposite direction or are they entirely different?

if they are the same you could try something like.

GML:
if key_right {
image_xscale = 1
}

if key_left {
image_xscale = -1
}
that should technically just invert the sprites image.
 
are both the right and left sprites the same just facing the opposite direction or are they entirely different?

if they are the same you could try something like.

GML:
if key_right {
image_xscale = 1
}

if key_left {
image_xscale = -1
}
that should technically just invert the sprites image.
No, they are different :) But thanks
 
Top