【问题标题】:how to move player by 1 tile libgdx如何将玩家移动 1 格 libgdx
【发布时间】:2014-07-06 17:45:46
【问题描述】:

我在 libgdx 中使用平铺。我正在做一个类似于最终幻想 1、2 等的自上而下的 rpg。

我用 setX(getX() + velocity.x * delta);和 setY(getY() + velocity.y * delta); 在地图上移动玩家。 我怎样才能让玩家逐块移动。以及我将如何检查哪个瓷砖是 玩家在?有人能帮我吗。谢谢。

我发现了这个:

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;
}

}

来自:libGDX: How to implement a smooth tile / grid based game character movement?

但我似乎可以在我的游戏中实现它。

【问题讨论】:

    标签: java libgdx desktop-application


    【解决方案1】:

    我正在编写一个 2048 游戏,我为移动玩家所做的是有两个 x 变量和两个 y 变量,一个用于坐标作为屏幕上的像素,另一个用于网格中。执行移动时,我更改网格变量并让我的游戏循环检查网格 x 和 y 变量乘以网格大小是否等于屏幕 x 和 y 变量。如果不匹配,则移动播放器。

    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 --;
        }
    }
    

    要查看玩家所在的图块,您只需执行 getX 和 getY 即可找到玩家的图块坐标。

    【讨论】:

    • 谢谢,我现在就试试。但为什么 x * gridsize?
    • x 是网格中的坐标,xx 是屏幕像素。所以 GridSize 将是 Grid 的每个图块有多大,所以 (x * GridSize) 通常等于 xx 如果玩家没有移动。
    • 啊,所以玩家将沿着 xx 移动。它会一直移动,直到它等于 x 即瓷砖数量 * 网格大小?
    • 注意Gridsize可以是任意数字,其中xx和yy改变的数字可以是任意可以划分为Gridsize的数字。例如网格大小为 9,xx + 3,因为 9 可以除以 3。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-13
    • 2023-02-25
    • 2017-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多