Bentley
Member
I'm trying to make it so the player can't go backwards. Ex: if he's moving east, he can't go west.
If the player is moving in direction 0, and sets target_dir to 180, then I can see why the exit doesn't run:
direction (-180) doesn't equal target_dir (180). So I get why the exit doesn't run, which allows the player to reverse direction. But then once the player is moving left, he's not able to move right. So in the second case, -360 doesn't equal 0, yet the exit statement runs, and he's not able to reverse direction. Direction resets? And if so, do I need to use abs(target_dir) so that -180 equals 180 which would prevent backwards movement?
As you can see, I don't have a grasp on this. If anyone can explain it, I'd greatly appreciate it. (Sorry for the rookie question : (
Code:
switch (keyboard_key)
{
case vk_right: target_dir = 0; break;
case vk_left: target_dir = 180; break;
case vk_up: target_dir = 90; break;
case vk_down: target_dir = 270; break;
}
if (snapped) //Player can only change directions when snapped, and that's when target_dir is tested
{
if (direction - 180 == target_dir) exit;
direction = target_dir;
}
direction (-180) doesn't equal target_dir (180). So I get why the exit doesn't run, which allows the player to reverse direction. But then once the player is moving left, he's not able to move right. So in the second case, -360 doesn't equal 0, yet the exit statement runs, and he's not able to reverse direction. Direction resets? And if so, do I need to use abs(target_dir) so that -180 equals 180 which would prevent backwards movement?
As you can see, I don't have a grasp on this. If anyone can explain it, I'd greatly appreciate it. (Sorry for the rookie question : (