2015-10-20 41 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; 
    } 
} 

對不起,對於任何人可能已經對此感到困惑,我得到了它感謝任何人試圖幫助!