【发布时间】: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 编辑问题以包含代码。
-
谢谢,我会尽我所能提供帮助...那是一些可怕的代码...