我害怕重新发明方向盘,我想知道:
使用libGDX在自顶向下的平铺(2D)地图上实现平滑的基于网格的游戏字符移动的最佳方法是什么?
只要按下箭头键(或触摸事件发生在字符的某一方向),字符应保持在瓷砖之间的平稳移动,并应在键/触释放时完成限制在网格位置的操作。移动应该独立于帧速率。
我很高兴看到一些已经实现的示例,它们可以被研究并导致正确的libGDX API使用。
发布于 2014-01-07 00:32:39
测试某个特定按钮是否被按下(或屏幕被触摸),如果是,在一个字段中设置正确的目标瓷砖(玩家将进入的瓷砖)并开始移动,此移动将仅在玩家处于下一个瓷砖时结束。当发生这种情况时,再次检查输入以保持移动(即重复)。
假设您的瓷砖宽度/高度为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;
}
}
此代码仅为正确的移动而简化,您也需要将其用于其他方向。
如果玩家没有移动,不要忘记再次轮询输入。
编辑:
密钥的InputPolling:
if (Gdx.input.isKeyPressed(Keys.DPAD_RIGHT)){
InputPolling用于触摸(凸轮是您的相机,touchPoint是存储非投影触摸坐标的Vector3,moverightBounds a (libgdx)矩形):
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
}
}
诺恩已经说过了,您可以在呈现方法中将增量作为参数,也可以使用以下方法在其他任何地方获得它:
Gdx.graphics.getDeltatime();
参考文献:
发布于 2014-01-07 07:54:45
这仅适用于一维,但我希望您能理解,通过检查方向键,加/减1,然后将其转换为int (或地板),您将得到下一个瓷砖。如果您释放键,您仍然有一个未到达的目标,该实体将继续移动,直到它到达目标瓷砖。我想应该是这样的:
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;
}
}
https://stackoverflow.com/questions/20961596
复制相似问题