I want to set the movement in my game to a grid where the players movement is locked and other keys are also locked untill the player moves to the next grid which yould be
y-16 pixels from the players position when facing north
y+16 pixels from the players position when facing south
x-16 pixels from the players position when facing west
x+16 pixels from the players position when facing east
Here is Radnens movement code
function game()
{
BindKey(KEY_UP, '', '');
BindKey(KEY_DOWN, '', '');
BindKey(KEY_LEFT, '', '');
BindKey(KEY_RIGHT, '', '');
}
function Update() {
var moving = false; //I added in this variable to check if the player is moving
if (IsKeyPressed(KEY_UP)) {
QueuePersonCommand("ghost", COMMAND_FACE_NORTH, true);
QueuePersonCommand("ghost", COMMAND_MOVE_NORTH, false);
moving = true;
}
else if (IsKeyPressed(KEY_DOWN )) {
QueuePersonCommand("ghost", COMMAND_FACE_SOUTH, true);
QueuePersonCommand("ghost", COMMAND_MOVE_SOUTH, false);
moving = true;
}
else if (IsKeyPressed(KEY_LEFT)){
QueuePersonCommand("ghost", COMMAND_FACE_WEST , true);
QueuePersonCommand("ghost", COMMAND_MOVE_WEST , false);
moving = true;
}
else if (IsKeyPressed(KEY_RIGHT)) {
QueuePersonCommand("ghost", COMMAND_FACE_EAST , true);
QueuePersonCommand("ghost", COMMAND_MOVE_EAST , false);
moving = true;
}
I would want to modify this so that when I am pressing a button, the movement is locked when I release the button.
The movement will unlock itself only under two conditions
there is a tile obstructing the way or the player has moved 16 pixels ( tilesize ) in that direction
I will need 4 variables, one to check the players position at all times, another to get the position of the next tile,a variable that checks if the players position matches with the intended position they should reach and a final variable that checks if that intended position in obstructed by any tile.
Movement should then be locked only until check is true and there are no obstructions.
------------------------------------------------------------------------------------------------------------------------------------------------------------------
Here is what I tried in the Update script
var px = GetPersonX("ghost");
var py = GetPersonY("ghost");
var ty = 0, tx = 0; //position of next tile in co-ordinates for x and y
if (moving == false) // if the player is not moving
{
check = false;
switch(GetPersonDirection("ghost")) {
case "north": ty = py - 16; break; //north -y
case "south": ty = py + 16; break; //south +y
case "west": tx = px - 16; break; //west -x
case "east": tx = px + 16;; break; //east x
}
}
for now all I could figure out where the variables.