GML [SOLVED]Sprite Facing Direction

E

ejoshuat

Guest
I can't find anything to actually help me, I have all the movement sorted out and that but Icant find any code in which will change the player or sprite in the facing direction, so when I move left and right it will face the same direction. HELP

This is the code for the step event so far its all in working order:

//Get the player's input
key_right = keyboard_check(vk_right);
key_left = -keyboard_check(vk_left);
key_jump = keyboard_check_pressed(vk_space);

//React to inputs
move = key_left + key_right;
hsp = move * movespeed;
if (vsp < 10) vsp += grav;

if (place_meeting(x,y+1,obj_FloorCENT))
{
vsp = key_jump * -jumpspeed
}

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

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

sp202

Member
sprite_index is what you're after. Set the sprite_index to left and right facing sprites accordingly when you're moving in either direction.
 

matharoo

Udemy Instructor
If you have your player sprite facing right, just use image_xscale to make it face left/right.
Code:
if (key_left) image_xscale = -1; else
if (key_right) image_xscale = 1;
 
E

ejoshuat

Guest
If you have your player sprite facing right, just use image_xscale to make it face left/right.
Code:
if (key_left) image_xscale = -1; else
if (key_right) image_xscale = 1;
Ill try that one sec
 
E

ejoshuat

Guest
If you have your player sprite facing right, just use image_xscale to make it face left/right.
Code:
if (key_left) image_xscale = -1; else
if (key_right) image_xscale = 1;
It didnt change the direction that its facing or anything it just stretched the image to be wider thats all that happened?
 

matharoo

Udemy Instructor
It didnt change the direction that its facing or anything it just stretched the image to be wider thats all that happened?
Well you could be using a different value for the xscale. Anyway, if you need to use sprite_index, you can just use:
Code:
if (key_left) sprite_index = <sprite_name>;
else
if (key_right) sprite_index = <sprite_name>;
This changes the sprite (here, sprite_index) to whatever sprite you want when you go in a direction. Change the <sprite_name> with the appropriate sprites and you're good to go.
 

JackTurbo

Member
I'm not sure why the options posted so far aren't working for you.

But another option is to use direction to test which way your character is facing.

Having had another gander at your code I realise now though, its probably easier to do it based off your move variable

Create event:
Code:
//moving right and not jumping o falling
if (move > 0 && vsp = 0){
    sprite_index = sRightRunSprite; 
    //replace this with what ever your actual sprite is
}

//moving left and not jumping or falling
if (move < 0 && vsp = 0){
    sprite_index = sLeftRunSprite; 
    //replace this with what ever your actual sprite is 
}

//jumping right
if (move > 0 && vsp > 0){
    sprite_index = sRightJumpSprite; 
    //replace this with what ever your actual sprite is 
}
//jumping left
if (move < 0 && vsp > 0){
    sprite_index = sLeftJumpSprite; 
    //replace this with what ever your actual sprite is 
}

//Falling right
if (move > 0 && vsp < 0){
    sprite_index = sRightFallSprite; 
    //replace this with what ever your actual sprite is 
}
//Falling left
if (move < 0 && vsp < 0){
    sprite_index = sLeftFallSprite; 
    //replace this with what ever your actual sprite is 
}
 
E

ejoshuat

Guest
I'm not sure why the options posted so far aren't working for you.

But another option is to use direction to test which way your character is facing.

Having had another gander at your code I realise now though, its probably easier to do it based off your move variable

Create event:
Code:
//moving right and not jumping o falling
if (move > 0 && vsp = 0){
    sprite_index = sRightRunSprite;
    //replace this with what ever your actual sprite is
}

//moving left and not jumping or falling
if (move < 0 && vsp = 0){
    sprite_index = sLeftRunSprite;
    //replace this with what ever your actual sprite is
}

//jumping right
if (move > 0 && vsp > 0){
    sprite_index = sRightJumpSprite;
    //replace this with what ever your actual sprite is
}
//jumping left
if (move < 0 && vsp > 0){
    sprite_index = sLeftJumpSprite;
    //replace this with what ever your actual sprite is
}

//Falling right
if (move > 0 && vsp < 0){
    sprite_index = sRightFallSprite;
    //replace this with what ever your actual sprite is
}
//Falling left
if (move < 0 && vsp < 0){
    sprite_index = sLeftFallSprite;
    //replace this with what ever your actual sprite is
}
Thanksss works a dream!
 

JackTurbo

Member
But how do I then get the animation to stop once the key has been released and the player has stopped moving?
Whoops, sorry I forgot to add in an idle state.

Stick this at the top of the code I posted earlier.

Code:
//Checks if you're not moving vertically or horizontally
if (move == 0 && vsp == 0){
    //checks if your direction is left
    if (direction >90 && direction < 270){
       sprite_index = sLefttIdleSprite;
    }else{
        sprite_index = sRightIdleSprite;
    }

}
 
E

ejoshuat

Guest
Whoops, sorry I forgot to add in an idle state.

Stick this at the top of the code I posted earlier.

Code:
//Checks if you're not moving vertically or horizontally
if (move == 0 && vsp == 0){
    //checks if your direction is left
    if (direction >90 && direction < 270){
       sprite_index = sLefttIdleSprite;
    }else{
        sprite_index = sRightIdleSprite;
    }

}
Thankssss man!
 
Top