R
Rackover
Guest
Hello,
I'm having my first steps on GameMaker and after following the tutorial to create a basic platformer game, everything works perfectly : I can move my character, jump, and I correctly collide with walls and ground.
But if try to add some conditions to change my sprite using sprite_index, for an unknown reason, my character gets stucks in the ground and cannot move until I press Jump and then, I can move while i'm in the air, but not on the ground.
Simply deleting the lines related to sprite_index fixed it.
After I studied the GM documentation and found no answer, I had a hard time suceeding to register on this website to ask this question :
Could anyone provide me more information about sprite_index and the way it interacts with physics or anything ? No one seems to mention the way it's working on my side.
Here is the working code :
By adding these 9 lines at the end, the bugs I talked about do happen :
I'm having my first steps on GameMaker and after following the tutorial to create a basic platformer game, everything works perfectly : I can move my character, jump, and I correctly collide with walls and ground.
But if try to add some conditions to change my sprite using sprite_index, for an unknown reason, my character gets stucks in the ground and cannot move until I press Jump and then, I can move while i'm in the air, but not on the ground.
Simply deleting the lines related to sprite_index fixed it.
After I studied the GM documentation and found no answer, I had a hard time suceeding to register on this website to ask this question :
Could anyone provide me more information about sprite_index and the way it interacts with physics or anything ? No one seems to mention the way it's working on my side.
Here is the working code :
Code:
///Get the player's input
//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_wall))
{
vsp = key_jump * -jumpspeed
}
//Horizontal Collision
if (place_meeting(x+hsp,y,OBJ_wall))
{
while(!place_meeting(x+sign(hsp),y,OBJ_wall))
{
x += sign(hsp);
}
hsp = 0;
}
x += hsp;
//Vertical Collision
if (place_meeting(x,y+vsp,OBJ_wall))
{
while(!place_meeting(x,y+sign(vsp),OBJ_wall))
{
y += sign(vsp);
}
vsp = 0;
}
y += vsp;
By adding these 9 lines at the end, the bugs I talked about do happen :
Code:
if (vsp!=0){
sprite_index=SPR_player_jumping;
}
else if (hsp!=0){
sprite_index=SPR_player_running;
}
else{
sprite_index=SPR_player;
}