MartinK12
Member
I use below super basic platformer move code in my on_ground state in user_event_0.
Simplified version of my code related to this problem:
What's the problem?
I've noticed then when player press key_right so he's moving right the hsp value is 4. But when player changes the direction of movement to opposite so he wants to go left - he release the key_right and then presses key_left the hsp will behave in two ways:
behavior 1:
-in current step hsp will be 4
-in the next step hsp will be 0
-and in another next step hsp will be -4
-game steps looks like this, ie.:
step 10 - hsp 4
step 11 - hsp 0
step 12 - hsp -4
behavior 2: this happens occasionally when players fast flips key right to key left
-in current step hsp will be 4
-in the next step hsp will be -4
-so in this behaviour hsp skips the value of 0,
-game steps looks like this, ie.:
step 10 - hsp 4
step 11 - hsp -4
But I want to do some stuff when player changes his direction and I want hsp to be 0, but this behavior skips it entirely. Of course this is also true for opposite direction so moving to left and changing direction to right. When I do this stuff slowly everything works fine.
I assume that hsp behaves in two ways bcs of the speed of key presses? But the code is super basic so shouldn't it behave exactly the same all the time, or how can I achieve 100% proper behavior all the time?
Thank You for all suggestions, links, videos
Simplified version of my code related to this problem:
Code:
key_left = keyboard_check(ord("A"));
key_right = keyboard_check(ord("D"));
var move = key_right - key_left;
hsp = move * move_speed; //move_speed = 4
//THIS IS PROBLEMATIC PART START
if (hsp == 0) {
//set sprite and sound for player idle
show_debug_message(hsp);
}
else { //else hsp <> 0 so we are running left or right
//set sprite and sound for player running
show_debug_message(hsp);
}
//THIS IS PROBLEMATIC PART END
if (hsp != 0) image_xscale = sign(hsp);
//horizontal collision
if (place_meeting(x+hsp,y,obj_wall)) {
while (!place_meeting(x+sign(hsp),y,obj_wall)) {
x = x + sign(hsp);
}
hsp = 0;
}
x = x + hsp;
I've noticed then when player press key_right so he's moving right the hsp value is 4. But when player changes the direction of movement to opposite so he wants to go left - he release the key_right and then presses key_left the hsp will behave in two ways:
behavior 1:
-in current step hsp will be 4
-in the next step hsp will be 0
-and in another next step hsp will be -4
-game steps looks like this, ie.:
step 10 - hsp 4
step 11 - hsp 0
step 12 - hsp -4
behavior 2: this happens occasionally when players fast flips key right to key left
-in current step hsp will be 4
-in the next step hsp will be -4
-so in this behaviour hsp skips the value of 0,
-game steps looks like this, ie.:
step 10 - hsp 4
step 11 - hsp -4
But I want to do some stuff when player changes his direction and I want hsp to be 0, but this behavior skips it entirely. Of course this is also true for opposite direction so moving to left and changing direction to right. When I do this stuff slowly everything works fine.
I assume that hsp behaves in two ways bcs of the speed of key presses? But the code is super basic so shouldn't it behave exactly the same all the time, or how can I achieve 100% proper behavior all the time?
Thank You for all suggestions, links, videos