0

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.

Community
  • 1
  • 1

1 Answers1

0

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.

Moddl
  • 360
  • 3
  • 13
  • Thankyou I'll try it now. but why x * gridsize? – user3810058 Jul 07 '14 at 01:00
  • x is the coordinates in the grid, xx is the screen pixel. so the GridSize would be how large each tile of the Grid is so (x * GridSize) normally would equal to xx if the player is not moved. – Moddl Jul 07 '14 at 04:12
  • ah so the player will move along xx. and it will move until it is equal to x which is the number of tiles * the gridsize? – user3810058 Jul 08 '14 at 03:04
  • Just to note the Gridsize can be any number and the number xx and yy is changed by can be any number that can be divided into Gridsize. For example Grid size is 9, xx + 3, because 9 can be divided by 3. – Moddl Jul 09 '14 at 03:35