Reputation: 3
I'm using tiled in libgdx. I'm doing a top down rpg similar to final fantasy 1, 2, etc.
I use setX(getX() + velocity.x * delta); and setY(getY() + velocity.y * delta); to move the player around the map. how can i make the player move tile by tile. and How will I check what tile is the player in? can someone help me. thankyou.
I found this :
void update()(
// Getting the target tile
if ( rightArrowPressed ) {
targetX = (int)(currentX + 1); // Casting to an int to keep
// the target to next tile
}else if ( leftArrowPressed ){
targetX = (int)(currentX - 1);
}
// Updating the moving entity
if ( currentX < targetX ){
currentX += 0.1f;
}else if ( currentX > targetX ){
currentX -= 0.1f;
}
}
from : libGDX: How to implement a smooth tile / grid based game character movement?
but I can seem to see away to implement it in my game.
Upvotes: 0
Views: 1435
Reputation: 360
I am programming a 2048 game and what i did to move the player is to have two x variables and two y variables, one for the coordinates as pixels on screen and other as in grid. when move is executed, i change the grid variable and have my gameloop checking if the grid x and y variable multiplied by the grid size equals the screen x and y variable. if it doesn't match, i then move the player.
int x, y;//Grid
int xx, yy;//Screen
int GridSize = 3;
public void tick(){
if(x * GridSize > xx){
xx ++;
}
if(x * GridSize < xx){
xx --;
}
if(y * GridSize > yy){
yy ++;
}
if(y * GridSize < yy){
yy --;
}
}
To check what tile the player is in, you just need to do getX and getY to find the tile coordinates of the player.
Upvotes: 0