简体   繁体   English

如何将播放器移动1个图块libgdx

[英]how to move player by 1 tile libgdx

I'm using tiled in libgdx. 我在libgdx中使用平铺。 I'm doing a top down rpg similar to final fantasy 1, 2, etc. 我正在执行类似最终幻想1、2等的自上而下的rpg。

I use setX(getX() + velocity.x * delta); 我使用setX(getX()+ velocity.x * delta); and setY(getY() + velocity.y * delta); 和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? 来自: libGDX:如何实现基于平铺/网格的平滑游戏角色移动?

but I can seem to see away to implement it in my game. 但我似乎看不到要在游戏中实现它。

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. 我正在编写2048游戏,而我移动玩家的方法是有两个x变量和两个y变量,一个用于屏幕上的像素坐标,另一个用于网格中的坐标。 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. 当执行移动时,我更改了网格变量,并让我的游戏循环检查了网格x和y变量乘以网格大小是否等于屏幕x和y变量。 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. 要检查播放器所在的图块,您只需执行getX和getY即可找到播放器的图块坐标。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM