• Hey! Guest! The 39th GMC Jam will take place between November 26th, 12:00 UTC and November 30th, 12:00 UTC. Why not join in! Click here to find out more!

GMS 2 Asymmetrical Sprite Jumping animation

gw4

Member
Hello!

I'm learning 2-D platformer basics from Spaulding's recent GMS2 YouTube tutorial and it's been going well but I ran into a hiccup.

My sprite carries a shield in his left hand so he looks different when traveling LT and RT.

How could I modify this code to have the character face left (JumpingLT) when jumping in the left direction?

Right now, when I jump toward the left, he faces right. I've tried several things to no avail.

---------Current code------------
//Animation//Air
{
if (!place_meeting(x,y+1,ground))
{
sprite_index = sprite_player_JumpingRT;
image_speed = 0;
image_index = 1;
}
}
-----------------------------------

Thanks for your time!
 

Nidoking

Member
Well, I don't see any attempt there to set the sprite to the facing left sprite under any conditions. That's something you might want to add.
 

Felbar

Member
are you trying to detect if you are moving left or right?

if (x > xprevious) //moving right

if(x < xprevious ) //moving left

if(x == xprevious) //not moving

there are better ways to do this but i would have to see your movement and input code
 

gw4

Member
This is what I've gotten so far:

// Core Player Logic

//Get player inputs

key_left = keyboard_check(vk_left);
key_right = keyboard_check(vk_right);
key_jump = keyboard_check_pressed(vk_space);

//Calculate movement
var _move = key_right - key_left;
hsp = _move *walksp;
vsp = vsp + grv;

if (place_meeting(x,y+1,ground)) && (key_jump)

{
vsp = -jumpsp
}



//Horizontal collision

if (place_meeting(x+hsp,y,ground))
{
while (!place_meeting(x+sign(hsp),y,ground))
{
x = x + sign(hsp);
}

hsp = 0;
}

x = x + hsp;



//Vertical collision
if (place_meeting(x,y+vsp,ground))

{
while (!place_meeting(x,y+sign(vsp),ground))
{
y = y + sign(vsp);
}
vsp = 0;
}
y = y + vsp;



//Animation//Ground
if (hsp = 0)
image_index = 0;
image_speed = 0;

{
if sprite_index = sprite_Player_JumpingLT
{
sprite_index = sprite_Player_FaceLT
image_index = 0;
image_speed = 0;
}

}
{

if sprite_index = sprite_Player_JumpingRT
{
sprite_index = sprite_Player_FaceRT
image_index = 0;
image_speed = 0;
}

}

if (hsp >0) sprite_index = sprite_Player_WalkRT;
image_speed = 1;
if (hsp <0) sprite_index = sprite_Player_WalkLT;
image_speed = 1;



//Animation//Air
if (!place_meeting(x,y+1,ground))

{
sprite_index = sprite_Player_JumpingRT;
image_speed = 0;
image_index = 1;

}
 

Felbar

Member
Code:
//Animation//Air
if (!place_meeting(x,y+1,ground))
{
if (sprite_index == sprite_Player_WalkRT or sprite_index == sprite_Player_FaceRT )  sprite_index = sprite_Player_JumpingRT;
if (sprite_index == sprite_Player_WalkLT or sprite_index == sprite_Player_FaceLT)  sprite_index = sprite_Player_JumpingLT;

//this should work with what you have but if you can move horizontally in the air you need to check for that too

if (hsp >0) sprite_index = sprite_Player_JumpingRT;
if (hsp <0) sprite_index = sprite_Player_JumpingLT;

image_speed = 0;
image_index = 1;
}
 
Last edited:

Felbar

Member
it would be better to add a facing variable to your player object
in create and set it based on your hsp like

//in create
facing = 1;


//then during movement code
if(hsp !=0) facing = sign(hsp);

then you will always know what dir you are facing

then you can just

Code:
//in air
if (!place_meeting(x,y+1,ground)) {
if(facing > 0) {
sprite_index = sprite_Player_JumpingRT;
}
else {
sprite_index = sprite_Player_JumpingLT;
}
}
the more animations you add how ever this is going to get to be a headache
you should look into adding states , and state machines
 

gw4

Member
I appreciate your time, Felbar!

I'll tinker with the suggestions you've provided and see how it goes. Will update soon.
 
Top