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

Trying to alter sprite idle animation to face left without using xscale.

I have a player sprite that's different depending on the side it's viewed from and I've been trying for 5 hours or so to get the idle left animation to stay in place after turning left.

Here is my step event's code. I'm doing this on mobile so I can't attach the file; obviously I got pretty fed up with sitting at my desk.

//Get Player Input
key_left = keyboard_check(ord("A"));
key_right = keyboard_check(ord("D"));
key_jump = keyboard_check_pressed(vk_space);

//Calculate Movement
var move = key_right - key_left;

var idle = keyboard_check(vk_nokey);

hsp = move * walksp;

vsp = vsp + grv;

if (place_meeting(x,y+1,oWall)) and (key_jump)
{
vsp = -8;

}
//Horizontal Collision
if (place_meeting(x+hsp,y,oWall))
{
while (!place_meeting(x+sign(hsp),y,oWall))
{
x = x + sign(hsp);
}
hsp = 0;
}
x = x + hsp;
//Vertical Collision
if (place_meeting(x,y+vsp,oWall))
{
while (!place_meeting(x,y+sign(vsp),oWall))
{
y = y + sign(vsp);
}
vsp = 0;
}
y = y + vsp;

//Walk & Jump Animation
if (!place_meeting(x,y+1,oWall))
{
sprite_index = sPlayerAlt;
image_speed = 1;
if (sign(vsp) > 0) image_index = 1; else image_index = 0;

}
else
{
if (hsp == 0)
{
sprite_index = sPlayer;
}
else
{
if (sprite_index!=sPlayerWR) sprite_index = sPlayerWR;
}
if (hsp < 0)
{
sprite_index = sPlayerWL;
}

}

Yes, I have been watching a bit of Shaun Spalding.

Any one have any ideas how I could adapt this to allow for my idle left animation to apply? If you think attaching the file would make it easier then I'll do so.
 

Alexx

Member
Have a look into using enums to keep track of what your player character is doing.
Failing that:

Try changing:
GML:
key_left = keyboard_check(ord("A"));
key_right = keyboard_check(ord("D"));
to
Code:
key_left = keyboard_check(ord("A"));
key_right = keyboard_check(ord("D"));
if key_left last_key="left";
if key_right last_key="right";

///pop this at the end of the event:
if !key_left && !key_right
{
   if last_key="left" sprite_index=spr_idle_left;
   if last_key="right" sprite_index=spr_idle_right;
}
and in the create event
Code:
last_key="left";
 

TheouAegis

Member
Even if you don't use image_xscale, you still need a variable to tell the instance to flip its sprite. Then every time you change the sprite, check that variable rather than whatever the current sprite is.
 
Have a look into using enums to keep track of what your player character is doing.
Failing that:

Try changing:
GML:
key_left = keyboard_check(ord("A"));
key_right = keyboard_check(ord("D"));
to
Code:
key_left = keyboard_check(ord("A"));
key_right = keyboard_check(ord("D"));
if key_left last_key="left";
if key_right last_key="right";

///pop this at the end of the event:
if !key_left && !key_right
{
   if last_key="left" sprite_index=spr_idle_left;
   if last_key="right" sprite_index=spr_idle_right;
}
and in the create event
Code:
last_key="left";
I've tried this out and thank god, it works. I've tried thirty different things and I was completely unaware that was an option. Thank you for the help.

Not to ask too much, how would I apply something similar to my jumping/falling animation? I.E, I have sprites for jumping/falling facing the left and vice versa.

I have a few ideas already but it would be nice to hear something that is likely to work.
 
Even if you don't use image_xscale, you still need a variable to tell the instance to flip its sprite. Then every time you change the sprite, check that variable rather than whatever the current sprite is.
Although I have found a solution, I'll take that into consideration in the future.
 
You probably don't want to change this when vsp is equal to zero. i.e. Only set image_index to zero if sign(vsp) < 0.
I was thinking about that but then I concluded on "If it works, it works." Probably not the best mindset, I'll be honest. Always good to know for the future though, so thanks.
 
Top