Hi, I am trying to make a 2d RPG similar to the old final fantasy's. I have a simple walking system, but for some reason whenever I press an arrow key, the sprite is delayed in walking in that direction for about a half a second, and sometimes, for example if I am going NORTH, then I press the left key to go WEST, the sprite for walking SOUTH will show up briefly, and eventually will cycle to walking left. What is causing this in my code?
#include "DarkGDK.h"
// the main entry point for the application is this function
// This is for the sprite player
int zeldaYloc=240;
int zeldaXloc=320;
int zeldaFacing=1;
// Constants for Tiles
const int GRASS = 5;
// Constants for Tile Sizes
const int TILE_WIDTH =32;
const int TILE_HEIGHT=32;
//Constants for the Player Sprite Sheet
const int PLAYER = 12;
const int PLAYER_ROWS = 4;
const int PLAYER_COLS = 4;
const int EAST_START = 1;
const int EAST_END = 4;
const int NORTH_START = 5;
const int NORTH_END = 8;
const int SOUTH_START = 9;
const int SOUTH_END = 12;
const int WEST_START = 13;
const int WEST_END = 16;
const int DELAY = 200;
// Constants for direction
const int NORTH = 1;
const int SOUTH = 2;
const int EAST = 3;
const int WEST = 4;
//Refresh Rate
const int REFRESH_RATE = 60;
// Constants for Tile Map Size
const int TILE_ROWS =15;
const int TILE_COLS =20;
// Function Prototypes
void setUp();
void displayTiles(int [][TILE_COLS], int);
bool arrowKeyPressed();
void updateDirection(int &);
void movePlayer(int);
//----------------------------------------------------------------------------------------
// Dark GDK Function. -
//----------------------------------------------------------------------------------------
void DarkGDK ( void )
{
// Here is the Array Map
int tileMap[TILE_ROWS][TILE_COLS] =
{ {GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS},
{GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS},
{GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS},
{GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS},
{GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS},
{GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS},
{GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS},
{GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS},
{GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS},
{GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS},
{GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS},
{GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS},
{GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS},
{GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS},
{GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS,GRASS}
};
//Player Start Direction
int direction = EAST;
// Perform all Setups
setUp();
// Game Loop
while ( LoopGDK() )
{
// Display Tiles
displayTiles(tileMap, TILE_ROWS);
updateDirection(direction);
// If Up,Down,Right,or Left keys are pressed
if ( arrowKeyPressed() )
{
// Update the direction.
updateDirection(direction);
// Move Player.
movePlayer(direction);
}
// Refresh the screen
dbSync();
}
}
//----------------------------------------------------------------------------------------
// setUp Function loads all the images. -
//----------------------------------------------------------------------------------------
void setUp()
{
// Loads the tiles images
dbLoadImage("grass.bmp", GRASS);
// Loads the sprites images
dbSetImageColorKey(255,0,1);
dbCreateAnimatedSprite(PLAYER, "ZeldaSpriteSheet.bmp", PLAYER_COLS, PLAYER_ROWS, PLAYER);
dbPlaySprite(PLAYER, EAST_START, EAST_END, DELAY);
dbSprite(PLAYER, 0, 0, PLAYER);
dbSyncOn();
dbSyncRate(REFRESH_RATE);
}
//----------------------------------------------------------------------------------------
// Display Tiles Fuction. -
//----------------------------------------------------------------------------------------
void displayTiles(int map[][TILE_COLS], int rows)
{
// Variables for the tile coords
int x = 0, y = 0;
// Display all the tiles in the map
for (int r = 0; r < rows; r++)
{
// Set x to 0.
x = 0;
// Display all the tiles in the row.
for (int c = 0; c < TILE_COLS; c++)
{
dbPasteImage( map[r][c], x, y );
x += TILE_WIDTH;
}
// Increase Y for next row.
y += TILE_HEIGHT;
}
}
bool arrowKeyPressed()
{
bool status = false;
if ( dbRightKey() || dbLeftKey() || dbUpKey() || dbDownKey() )
{
status = true;
}
return status;
}
void updateDirection(int &direction)
{
if ( dbRightKey() )
{
direction = EAST;
}
else if ( dbLeftKey() )
{
direction = WEST;
}
else if ( dbUpKey() )
{
direction = NORTH;
}
else if ( dbDownKey() )
{
direction = SOUTH;
}
}
void movePlayer(int direction)
{
int x = dbSpriteX(PLAYER);
int y = dbSpriteY(PLAYER);
int startFrame, endFrame;
switch (direction)
{
case NORTH:
dbPrint("Going North");
y--;
startFrame = NORTH_START;
endFrame = NORTH_END;
updateDirection(direction);
break;
case SOUTH:
y++;
startFrame = SOUTH_START;
endFrame = SOUTH_END;
updateDirection(direction);
break;
case EAST:
x++;
startFrame = EAST_START;
endFrame = EAST_END;
updateDirection(direction);
break;
case WEST:
x--;
startFrame = WEST_START;
endFrame = WEST_END;
updateDirection(direction);
break;
}
updateDirection(direction);
dbPlaySprite(PLAYER, startFrame, endFrame, DELAY);
dbSprite(PLAYER, x, y, PLAYER);
}