测试是否按下了特定的按钮(或触摸了屏幕),如果是,则在一个区域中设置正确的目标图块(玩家将要去的图块)并开始移动到那里,只有当玩家在下一个图块中。发生这种情况时,请再次检查输入以继续移动(即重复)。
假设您的图块宽度/高度为 1,并且您希望每秒移动 1 个图块。您的用户按下了右箭头键。然后,您只需将 targettile 设置为播放器右侧的图块。
if(targettile!=null){
yourobject.position.x += 1*delta;
if(yourobject.position.x>=targettile.position.x){
yourobject.position.x = targettile.position.x;
targettile = null;
}
}
此代码仅针对向右移动进行了简化,您也需要针对其他方向进行此代码。
如果玩家没有移动,不要忘记再次轮询输入。
编辑:
输入轮询键:
if (Gdx.input.isKeyPressed(Keys.DPAD_RIGHT)){
InputPolling for touchs(cam 是你的相机,touchPoint 是一个 Vector3 来存储未投影的触摸坐标,moverightBounds 一个 (libgdx) Rectangle):
if (Gdx.input.isTouched()){
cam.unproject(touchPoint.set(Gdx.input.getX(), Gdx.input.getY(), 0));
//check if the touch is on the moveright area, for example:
if(moveRightBounds.contains(touchPoint.x, touchPoint.y)){
// if its not moving already, set the right targettile here
}
}
noone 已经说过了,您可以在 render 方法中将 delta 作为参数获取,或者您可以在其他任何地方使用:
Gdx.graphics.getDeltatime();
参考文献: