TheNegroShoddy
Member
Hello, I'm having an issue with my four-directional grid movement code. As you can see in the video below, there is something weird going on with the movement. At certain point near the end I get stuck when reaching the edge of the screen. The movement in general looks weird when the movement keys are held down and not simply pressed. Any ideas how to fix this?
srcMovePlayer()
Code:
Create Event:
execute code:
/// Initialize General variables
// Define the current location of the player on the grid
gridX = ( x div global.cellWidth ); // Cell within the row that the player is on (going horizontally) - starting from 0
gridY = ( y div global.cellHeight ); // Cell within the column that the player is on (going vertically) - starting from 0
// Define variables to store the position the player is moving from on the grid.
gridFromX = gridX; // Set to gridX by default since the player is not in the process of moving by default.
gridFromY = gridY; // Set to gridY by default since the player is not in the process of moving by default.
// Define variables to store where the player is moving to on the grid.
gridToX = gridX; // Set to gridX by default since the player is not in the process of moving by default.
gridToY = gridY; // Set to gridY by default since the player is not in the process of moving by default.
//
pState = PLAYER_ST.IDLE; // The state the player is in
pDir = DIR.DOWN; // Direction the player is moving in/facing
//
walkAniLength = 0.5; // The time it will take to cross one cell in seconds.
walkAniTime = 0; // How much of the animation has played out in seconds.
//
walkSpd = 4;
runSpd = walkSpd * 2;
spd = walkSpd;
Step Event:
execute code:
/// Player States
switch( pState )
{
case PLAYER_ST.IDLE:
if global.charControlEnabled == true && global.inputDisabled == false && global.gamePaused == false
{
if keyboard_check( global.leftKey ) && gridX > 0 { srcMovePlayer( DIR.LEFT ); exit; }
if keyboard_check( global.rightKey ) { srcMovePlayer( DIR.RIGHT ); exit; }
if keyboard_check( global.upKey ) && gridY > 0 { srcMovePlayer( DIR.UP ); exit; }
if keyboard_check( global.downKey ) { srcMovePlayer( DIR.DOWN ); exit; }
}
break;
case PLAYER_ST.WALKING:
var walkTimeLeft = walkAniTime / walkAniLength;
var movingGridX = lerp( gridFromX, gridToX, walkTimeLeft );
var movingGridY = lerp( gridFromY, gridToY, walkTimeLeft );
// Gradually increase the walkAniTime variable to indicate how many seconds the player walking animation has played.
walkAniTime += spd * ( 1 / room_speed );
// Set the new x and y coordinates by multiplying the value between two points interporlated by the walking time by the cell width/height.
x = ( movingGridX * global.cellWidth );
y = ( movingGridY * global.cellHeight );
if walkTimeLeft >= 1
{
walkAniTime = 0; // Reset variable
walkTimeLeft = 1; // Ensure variable doesn't exceed 1
pState = PLAYER_ST.IDLE; // Change state back to idle
}
break;
}
Code:
/// srcMovePlayer( DIRECTION )
/*
This script sets the player's grid related position variables and changes the state and direction of the player object prior to moving.
Pass enum DIR as an argument based on the key pressed by the player.
The method used to move the player is similar to doing:
gridToX = gridX + ( keyboard_check( global.leftKey ) - keyboard_check( global.rightKey ) );
gridToY = gridY + ( keyboard_check( global.upKey ) - keyboard_check( global.downKey ) );
*/
var inputDir = argument0; // The direction the player should move in based on the player's input of either left, right, up, or down.
var addToGridX = 0; // Must be 0 by default.
var addToGridY = 0; // Must be 0 by default.
// Change the value of either addToGridX or addToGridY depending on the key pressed by the player.
if inputDir == DIR.LEFT { addToGridX = -1; }
if inputDir == DIR.RIGHT { addToGridX = 1; }
if inputDir == DIR.UP { addToGridY = -1; }
if inputDir == DIR.DOWN { addToGridY = 1; }
// Set the variables indicating where the player is moving from to the player's current position within the grid.
gridFromX = gridX;
gridFromY = gridY;
// Set the variables indicating where the player is moving to within the grid.
gridToX = gridX + addToGridX;
gridToY = gridY + addToGridY;
// Set the current position of the player within the grid.
gridX = gridToX;
gridY = gridToY;
// Change the direction and player state.
pDir = inputDir;
pState = PLAYER_ST.WALKING;
Last edited: