【问题标题】:Java 2D Game 'Grid Movement'Java 2D 游戏“网格运动”
【发布时间】:2014-05-06 02:49:36
【问题描述】:

我正在使用 libGDX 尝试制作一个自上而下的 2D 游戏,并沿着 Pokemon 的线条移动。但是,我不确定如何实现。

我希望玩家能够按下一个键(即:箭头键或 WASD)来移动,但是,当玩家释放键时,我不希望角色在其位置下降之前停止移动沿着由 32x32 像素正方形组成的单元格网格。

那么我将如何创建一个 32x32 像素单元格的“不可见”网格,并让我的角色只有在其中一个单元格顶部时才停止移动?

到目前为止,我只进行了逐像素移动。哪一个,我考虑过我可以只用一个布尔值来说明角色是否在移动,如果角色落在正确的区域就改变它,但我还没有想到任何方法来实现它。

这是我用于逐像素移动的方法(libGDX 涵盖了键输入,它根据玩家边界框的位置绘制精灵):

public void generalUpdate() {
    if(Gdx.input.isKeyPressed(Keys.A) || Gdx.input.isKeyPressed(Keys.LEFT)) {
        if (anim_current == Assets.anim_walkingLeft)
            if(player.collides(wall.bounds)) {
                player.bounds.x += 1;
            }
            else
                player.dx = -1;
        else
            anim_current = Assets.anim_walkingLeft;
        Assets.current_frame = anim_current.getKeyFrame(stateTime, true);
    }
    else if (Gdx.input.isKeyPressed(Keys.D) || Gdx.input.isKeyPressed(Keys.RIGHT)) {
        if (anim_current == Assets.anim_walkingRight)
            if(player.collides(wall.bounds)) {
                player.bounds.x -= 1;
            }
            else
                player.dx = 1;
        else
            anim_current = Assets.anim_walkingRight;
        Assets.current_frame = anim_current.getKeyFrame(stateTime, true);
    }
    else if (Gdx.input.isKeyPressed(Keys.W) || Gdx.input.isKeyPressed(Keys.UP)) {
        if (anim_current == Assets.anim_walkingUp)
            if(player.collides(wall.bounds)) {
                player.bounds.y += 1;
            }
            else
                player.dy = -1;
        else
            anim_current = Assets.anim_walkingUp;
        Assets.current_frame = anim_current.getKeyFrame(stateTime, true);
    }
    else if (Gdx.input.isKeyPressed(Keys.S) || Gdx.input.isKeyPressed(Keys.DOWN)) {
        if (anim_current == Assets.anim_walkingDown)
            if(player.collides(wall.bounds)) {
                player.bounds.y -= 1;
            }
            else
                player.dy = 1;
        else
            anim_current = Assets.anim_walkingDown;
        Assets.current_frame = anim_current.getKeyFrame(stateTime, true);
    }
    else {
        Assets.current_frame = anim_current.getKeyFrames()[0];
        player.dx = player.dy = 0;
    }

    player.bounds.x += player.dx;
    player.bounds.y += player.dy;
}

【问题讨论】:

  • 到目前为止你尝试过什么?如果您愿意付出努力,大多数人都会很乐意帮助您。
  • @Anubian Noob 好吧,我是一个思想迟钝的人,我没有尝试过任何涉及基于网格的运动的东西,因为我没有想到任何可能有用的东西。
  • 包括您的(相关)代码以进行逐像素移动。
  • @Anubian Noob 编辑问题以包含代码。
  • 谢谢,我会尽我所能提供帮助...那是一些可怕的代码...

标签: java libgdx 2d top-down


【解决方案1】:

您的代码一团糟,我不想尝试修复它,但通常可以通过以下方式解决您的问题:

有一个布尔值来表示你是否正在移动。我们称之为isMoving。还有一种存储方向的方法(可能是0到3之间的整数?)。我们就叫它dir吧。


当您按下某个键时,将 isMoving 设置为 true。如果您与网格对齐,请将dir 更新为您按下的键的方向。

如果您没有持有密钥,请将 isMoving 设置为 false。

当你开始移动你的角色时,如果isMoving 为真并且它们没有与网格对齐,那么移动就是dir 所代表的方向。如果它们与网格对齐,则将isMoving 设置为 false 并且不要移动。


// Only including one key, process is same for others.
if (rightKey) {
    isMoving = true;
    if (aligned)
        dir = 0;
} /* chain other keys in here */ else {
    isMoving = false;
}

if (isMoving) {
    if (!aligned) {
        move(dir);
        render(dir);
    } else {
    }
}

我刚刚意识到它在当前状态下可能无法正常工作:/。我希望这至少能让你知道该怎么做。我会尝试再修改一些...

【讨论】:

  • 这个想法是正确的,尽管首先你需要将你的世界分割成一个网格以便它均匀映射(例如,如果你的角色在单元格 1, 5 你知道它们在像素坐标 1x32, 5x32 = 32, 160),向下移动一个单元格角色需要移动到 1x32, 6x32 = 1, 192
  • 一个更好的解决方案是在每个更新周期将你的角色沿轴移动 1 个像素,然后一旦释放按钮,将角色“捕捉”到适当的图块(应用关于 @ 987654330@ 网格,例如,您可以获得玩家当前像素位置32, 155,然后除以网格大小,因此网格32/1, 155/32 = 1, ~4.8 我们总是将其向上舍入到下一个网格,如果它是小数,那么它们应该以像素为单位对齐到网格1, 5 = 32, 160
  • 单独测试位置几乎可以工作......除了每当我调用我的方法来测试我的角色是否对齐时,它总是说它是错误的。我将返回 (0 == ((int) bounds.y) % 32); 是真还是假。除非我对浮点到整数转换的理解是错误的,否则它应该测试并返回当前像素是否能被 32 整除。
  • 每次更新我只让我的角色移动 1 个像素。它还在每次更新期间测试对齐方式。所以它应该按照if (isMoving) { move 1 pixel; if (!aligned) move 1 pixel; else set isMoving to false;} 的方式做一些事情
  • 没关系。我想到了。感谢大家的帮助!
猜你喜欢
  • 2014-01-21
  • 1970-01-01
  • 2016-05-23
  • 1970-01-01
  • 2010-10-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-01
相关资源
最近更新 更多