Your code is a huge mess, and I don't want to try to fix it, but here's generally how you can approach your problem:
Have a boolean to represent if you are moving. Let's call it isMoving
. Also have a method to store direction (maybe as an integer between 0 and 3?). Let's call it dir
.
When you press a key, set isMoving
to true. If you are aligned with the grid, update dir
to the direction of the key you press as well.
If you aren't holding keys, set isMoving
to false.
When you get to moving your character, if isMoving
is true and they are not aligned with the grid, then move is the direction represented by dir
. If they are aligned with the grid, then set isMoving
to false and don't move.
// Only including one key, process is same for others.
if (rightKey) {
isMoving = true;
if (aligned)
dir = 0;
} /* chain other keys in here */ else {
isMoving = false;
}
if (isMoving) {
if (!aligned) {
move(dir);
render(dir);
} else {
}
}
I just realized nearing the end that it might not work in it's current state :/. I hope this at least gives you an idea of what to do. I'll try to revise it some more...