CaptainHitam
Member
Hello! I found something interesting while messing around with my code.
Here it is, clearly following one of Shaun Spaulding's tutorials:
In the Step Event:
All it does is just move the player left and right, and it stops when it collides with a wall. This works just fine! But then I remembered that collision checks are expensive and that storing them in a variable is cheaper.
However when I do this:
The player gets stuck. Presumably because it has gone 4 pixels in to the wall, thus making the "onwall" condition always true.
So my question is why does storing collision checks in a variable cause it to have some sort of delay? Should I even store them in a variable? I'm also a beginner to coding in general, so it wouldn't surprise me if the problem lies within my own logic
Here it is, clearly following one of Shaun Spaulding's tutorials:
In the Step Event:
GML:
#region//set controls
key_left = (keyboard_check(vk_left)) || (keyboard_check(ord("A")));
key_right = (keyboard_check(vk_right)) || (keyboard_check(ord("D")));
#endregion
#region //set variables
var onwall = place_meeting(x+hsp,y,obj_wall);
#endregion
#region //movement code
var move = key_right - key_left;
hsp = move * maxspd;
#endregion
//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;
However when I do this:
GML:
//Horizontal Collision
if (onwall) {
while (!place_meeting(x+(sign(hsp)),y,obj_wall)) {
x = x+(sign(hsp));
}
hsp = 0;
}
So my question is why does storing collision checks in a variable cause it to have some sort of delay? Should I even store them in a variable? I'm also a beginner to coding in general, so it wouldn't surprise me if the problem lies within my own logic