测试是否按下(或触摸屏幕)某个特定按钮,如果是,则在一个字段中设置正确的目标图块(玩家将要去的图块)并开始移动到那里,此运动仅在玩家处于下一个瓷砖.当发生这种情况时,再次检查输入以保持移动(即重复).
让我们假设,您的图块宽度/高度为1,并且您希望每秒移动1个图块.您的用户按右箭头键.然后你只需将目标设置到玩家右侧的方块即可.
if(targettile!=null){
yourobject.position.x += 1*delta;
if(yourobject.position.x>=targettile.position.x){
yourobject.position.x = targettile.position.x;
targettile = null;
}
}
Run Code Online (Sandbox Code Playgroud)
此代码仅针对右移动进行了简化,您还需要将其用于其他方向.
如果玩家没有移动,请不要忘记再次轮询输入.
编辑:
输入密钥输入:
if (Gdx.input.isKeyPressed(Keys.DPAD_RIGHT)){
Run Code Online (Sandbox Code Playgroud)
用于触摸的InputPolling(cam是您的相机,touchPoint是用于存储未投影触摸坐标的Vector3,以及moverightBounds a(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
}
}
Run Code Online (Sandbox Code Playgroud)
并且没有人说过,你在渲染方法中得到delta作为参数,或者你可以在其他任何地方使用:
Gdx.graphics.getDeltatime();
Run Code Online (Sandbox Code Playgroud)
参考文献:
| 归档时间: |
|
| 查看次数: |
5047 次 |
| 最近记录: |