RenoAssassin
Member
Hello, I am having the most frustrating time trying to understand what I am doing wrong in my code, because the collision works sometimes and sometimes it does not. for example, sometimes I get stuck in the ground right at game start. And i have noticed this only happens when I apply a animation to the player but when I remove the animation the collision works fine. Here is the code I am using. any help is appreciated. PS. I am new when it comes to posting for help on forums so have no Idea if I am posting incorrectly let me know.
/// @description Insert description here
// core player logic
// get player input
key_left = keyboard_check(ord("A"));
key_right = keyboard_check(ord("D"));
key_jump = keyboard_check_pressed(vk_space);
// Caculate movement
var move = key_right - key_left;
hsp = move * walkspd;
vsp = vsp + grv;
if (place_meeting(x,y +1,wall)) and (key_jump)
{
vsp = -jumpsp
}
// Horrizontal Colision
if (place_meeting(x+hsp,y,wall))
{
while(!place_meeting(x+sign(hsp),y,wall))
{
x = x + sign(hsp);
}
hsp = 0;
}
x = x + hsp;
// vertical Colision
if (place_meeting(x,y+vsp,wall))
{
while (!place_meeting(x,y+sign(vsp),wall))
{
y = y + sign(vsp);
}
vsp = 0;
}
y = y + vsp;
// animation
if (!place_meeting(x,y+1,wall))
{
sprite_index = Knight_jump;
image_speed = 1;
if (vsp > 0) image_index = 1; else image_index = 0
}
else
{
image_speed =1
if (hsp == 0){
sprite_index = Knight_idle2;
}
else
{
image_speed =1
sprite_index = Knight_run2;
}
}
/// @description Insert description here
// core player logic
// get player input
key_left = keyboard_check(ord("A"));
key_right = keyboard_check(ord("D"));
key_jump = keyboard_check_pressed(vk_space);
// Caculate movement
var move = key_right - key_left;
hsp = move * walkspd;
vsp = vsp + grv;
if (place_meeting(x,y +1,wall)) and (key_jump)
{
vsp = -jumpsp
}
// Horrizontal Colision
if (place_meeting(x+hsp,y,wall))
{
while(!place_meeting(x+sign(hsp),y,wall))
{
x = x + sign(hsp);
}
hsp = 0;
}
x = x + hsp;
// vertical Colision
if (place_meeting(x,y+vsp,wall))
{
while (!place_meeting(x,y+sign(vsp),wall))
{
y = y + sign(vsp);
}
vsp = 0;
}
y = y + vsp;
// animation
if (!place_meeting(x,y+1,wall))
{
sprite_index = Knight_jump;
image_speed = 1;
if (vsp > 0) image_index = 1; else image_index = 0
}
else
{
image_speed =1
if (hsp == 0){
sprite_index = Knight_idle2;
}
else
{
image_speed =1
sprite_index = Knight_run2;
}
}