Legacy GM Player Not animating when left is press(Solved)

M

Mighty Honey Games

Guest
Help!!I have set it so that when my character moves it animate,but when my player goes left it doesn't animate.
Here is the code:

//Animate
if (key_right)
{
sprite_index = spr_player;
image_speed = .5;
}
//Move Left
if (key_left)
{
sprite_index = spr_player_left;
image_speed = .5;
}
//Jump
if (key_jump)
{
sprite_index = spr_player_jump;
image_speed = .5;
}

//Stop Animating
if (!key_left and !key_right and !key_jump)
{
image_speed = 0;
image_index = 0;
}
 
If the sprite works fine when going to the right, then I would believe that the left sprite itself might not have animation.
 
M

Mighty Honey Games

Guest
If that's true, and the sprite is shown as animated in the sprite's preview, then I would have to say something somewhere else is affecting the animation. From the code you posted, it should operate fine.
Here is the entire code:
if (global.pause) exit;

///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_ground))
{
vsp = key_jump * -jumpspeed
}
if (place_meeting(x,y+1,obj_dirt))
{
vsp = key_jump * -jumpspeed
}

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

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

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

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

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

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

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

y += vsp;

//Death
if (obj_player_stats.hp <= 0)
{
instance_destroy();
game_restart();
}

if (room = level0)
{
if (place_meeting(x,y,obj_door))
{
room_goto(level2);
}
}

//Animate
if (key_right)
{
sprite_index = spr_player;
image_speed = .5;
}
//Move Left
if (key_left)
{
sprite_index = spr_player_left;
image_speed = .5;
}
//Jump
if (key_jump)
{
sprite_index = spr_player_jump;
image_speed = .5;
}

//Stop Animating
if (!key_left and !key_right and !key_jump)
{
image_speed = 0;
image_index = 0;
}
 
Here is the entire code:
if (global.pause) exit;

///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_ground))
{
vsp = key_jump * -jumpspeed
}
if (place_meeting(x,y+1,obj_dirt))
{
vsp = key_jump * -jumpspeed
}

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

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

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

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

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

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

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

y += vsp;

//Death
if (obj_player_stats.hp <= 0)
{
instance_destroy();
game_restart();
}

if (room = level0)
{
if (place_meeting(x,y,obj_door))
{
room_goto(level2);
}
}

//Animate
if (key_right)
{
sprite_index = spr_player;
image_speed = .5;
}
//Move Left
if (key_left)
{
sprite_index = spr_player_left;
image_speed = .5;
}
//Jump
if (key_jump)
{
sprite_index = spr_player_jump;
image_speed = .5;
}

//Stop Animating
if (!key_left and !key_right and !key_jump)
{
image_speed = 0;
image_index = 0;
}
I would suggest surrounding your code with code tags, it makes it easier to read :D
You can do that by selecting "Insert" and "Code" from the editing toolbar that is above the area where you write your reply. Or you can manually type out the tags.
Also, I cannot see anything wrong with what you have posted.
I would suggest debugging and setting a break point at the line:
Code:
 sprite_index = spr_player_left;
Then, single stepping through to make sure nothing else is altering the image_speed variable.

Something else I might even try is to remove the section that stops animating.
I would then go ingame, and move my player to the left to change their sprite to spr_player_left.
One of two things will happen:
1. If the sprite still stops moving, then we know it is not caused by that section and must search elsewhere.
2. If the sprite does not stop moving, then we know that it is being stopped by that section.
 
M

Mighty Honey Games

Guest
I would suggest surrounding your code with code tags, it makes it easier to read :D
You can do that by selecting "Insert" and "Code" from the editing toolbar that is above the area where you write your reply. Or you can manually type out the tags.
Also, I cannot see anything wrong with what you have posted.
I would suggest debugging and setting a break point at the line:
Code:
 sprite_index = spr_player_left;
Then, single stepping through to make sure nothing else is altering the image_speed variable.

Something else I might even try is to remove the section that stops animating.
I would then go ingame, and move my player to the left to change their sprite to spr_player_left.
One of two things will happen:
1. If the sprite still stops moving, then we know it is not caused by that section and must search elsewhere.
2. If the sprite does not stop moving, then we know that it is being stopped by that section.
Fixed the problem all I had to do was change this code
Code:
move = key_left + key_right
to
Code:
move = key_right - key_left
and change this code
Code:
key_right = keyboard_check(vk_right);
key_left = -keyboard_check(vk_left);
to
Code:
key_right = keyboard_check(vk_right);
key_left = keyboard_check(vk_left);
notice how I got rid of the - sign in
Code:
key_left = keyboard_check(vk_left);
 
Top