2015-10-20 40 views
2

我的玩家出于某种原因能够穿过它不应该穿过的物体。我正在使用一个2d布尔数组,如果网格中剩下的球员的位置是真的,那么他不应该能够移动,并且同样是正确的。我知道碰撞处理程序正在工作,因为它所做的只是检查左侧或右侧是否存在某些内容,并且是否写入了player.setCanMoveLeft或者对错,代码在以前的版本中工作。当玩家的左侧或右侧出现某些东西时,我会打印出一些东西,以便我知道碰撞处理程序正在完成这项工作。我只是不明白这里发生了什么是球员延伸,我的实体更新方法玩家穿越物体时,它不应该是libgdx java

if(!wantsToMoveLeft && !wantsToMoveRight) 
    velocity.x = 0; 

if(velocity.x > 1){ 
    velocity.x = 1; 
}if(velocity.x<-1) 
    velocity.x = -1; 

position.x += velocity.x; 
spritePosition.x += velocity.x; 
position.y += velocity.y; 
spritePosition.y += velocity.y; 

,这里是我的球员更新的方法,

if(wantsToMoveRight && canMoveRight) 
    velocity.x = 1; 
if(wantsToMoveLeft && canMoveLeft) 
    velocity.x = -1; 

if(wantsToJump && canJump){ 
    canFall = true; 
    velocity.y += 1f; 

    if(lastLeft){ 
     jumpLeft = true; 
     jumpRight = false; 
    }else{ 
     jumpRight = true; 
     jumpLeft = false; 
    } 
}else if(canJump == false){ 
    jumpLeft = false; 
    jumpRight = false; 
} 

super.update(); 

这里是我的输入监听器类太

@Override 
public boolean keyDown(int keycode) { 
    if(keycode == Keys.A){ 
     player.setLastLeft(true); 
     player.setLastRight(false); 
     player.setWantsToMoveLeft(true); 
     player.setWantsToMoveRight(false); 
    } 

    if(keycode == Keys.D){ 
     player.setLastRight(true); 
     player.setLastLeft(false); 
     player.setWantsToMoveRight(true); 
     player.setWantsToMoveLeft(false); 
    } 

    if(keycode == Keys.W){ 
     player.setWantsToJump(true); 
    } 
    return false; 
} 

@Override 
public boolean keyUp(int keycode) { 
    if(keycode == Keys.A){ 
     player.setLastLeft(true); 
     player.setLastRight(false); 
     player.setWantsToMoveLeft(false); 
    } 
    if(keycode == Keys.D){ 
     player.setLastRight(true); 
     player.setLastLeft(false); 
     player.setWantsToMoveRight(false); 
    } 

    if(keycode == Keys.W){ 
     player.setWantsToJump(false); 
    } 
    return false; 
} 

如果任何人都可以帮助我,我会非常感激,因为我完全丧失。如果您需要任何其他信息,请在评论中提出。谢谢!!

更新 - 如果我走右边附近的对象(击球前)和stop(让关键的GO)然后试图压制它,它不会让我感动(即它的工作原理,如果我这样做)

- 这个代码我之前我相信工作切换到使用InputListener这可能是假的,虽然我不太记得了,但我知道肯定是没有工作,因为我切换从玩家使用Gdx.input更新到通信通过一个InputListener

回答

1

我错过了一个简单的解决方案与不执行停止时告诉他不能移动。我无法解释为什么它以前工作,现在我需要这行代码,但在这里,我将这些代码行放在我的播放器更新和方法中,现在它运行得非常好。

if(!canMoveLeft) { 
    if(!wantsToMoveRight) { 
     velocity.x = 0; 
    } else { 
     velocity.x = 1; 
    } 
} 

if(!canMoveRight) { 
    if(!wantsToMoveLeft) { 
     velocity.x = 0; 
    } else { 
     velocity.x = -1; 
    } 
} 

对不起,对于任何人可能已经对此感到困惑,我得到了它感谢任何人试图帮助!