Reputation: 97
Okay, I'm having problems with my sprites facing direction, I have so that: If i press the D key, the sprite will play the walking sprite and when i release it, it faces in the direction i was walking, HOWEVER when i'm walking left(A key), it plays the sprite walking left animation but when i stop, it looks the other direction instantly, so heres my code (PS i have a sprite that isnt walking and facing the other direction (player_other_direction)):
///Platformer Physics
var rkey = keyboard_check(ord("D"));
var lkey = keyboard_check(ord("A"));
var jkey = keyboard_check(ord("W"));
//Check for the ground
if(place_meeting(x, y+1, obj_platform))
{
vspd = 0;
//Jumping
if(jkey)
{
vspd = -jspd;
}
}
else
{
//Move down with gravity
if (vspd < 10)
{
vspd += grav;
}
if(keyboard_check_released(ord("W")) && vspd <-4){
vspd = -4;
}
}
//Moving to the right
if(rkey)
{
sprite_index = player_walking_right;
if(hspd < spd){
hspd += fric;
} else{
hspd = spd;
}
}
//Moving to the left
if(lkey)
{
sprite_index = player_walking_left;
if(hspd > -spd){
hspd -= fric;
} else {
hspd = -spd;
}
}
//Check for not moving
if((!rkey and !lkey) || (rkey && lkey))
{
sprite_index = player;
if(hspd != 0){
if(hspd<0){
hspd += fric;
} else {
hspd -= fric;
}
}
}
//Horizontal Collisions
if(place_meeting(x+hspd, y, obj_platform))
{
while(!place_meeting(x+sign(hspd), y, obj_platform)){
x+= sign(hspd);
}
hspd = 0;
}
//Move Horizontally
x += hspd;
//Vertical Collisions
if(place_meeting(x, y+vspd, obj_platform))
{
while(!place_meeting(x, y+sign(vspd), obj_platform)){
y+= sign(vspd);
}
vspd = 0;
}
//Move Vertically
y += vspd;
PS. I have define the variables, however they are in another script
Upvotes: 2
Views: 1070
Reputation: 1274
//Check for not moving
if((!rkey and !lkey) || (rkey && lkey))
{
sprite_index = player;
if(hspd != 0){
if(hspd<0){
hspd += fric;
} else {
hspd -= fric;
}
}
}
Here, you check if the player is moving; if not, you set its sprite_index to "player". My guess is that "player" faces to the right, so when you stop moving, you will face the right no matter which way you were walking.
You'll need to make a way to check in which direction you were last moving, and if it was to the left, set sprite_index to "player_other_direction" instead of "player" (since you mentioned you already had this "player_other_direction" sprite ready to be used).
Upvotes: 3