2012-10-12 40 views
0

我目前正在研究一個相對簡單的平臺遊戲,它有一個奇怪的錯誤。你通過墮落地面開始遊戲(你在地面上生成幾塊),但是當你着地時,你的腳陷入內心世界,直到你跳躍才能移動。這就是我的意思是:碰撞檢測錯誤

http://i.imgur.com/IKLZY.png

球員的腳都低於地面幾個像素。但是,這個問題只發生在整個地圖的3個地方,並且只出現在這3個地方。我假設這個問題存在於我的碰撞檢測代碼中,但我不完全確定,因爲當它發生時我不會發生錯誤。

public boolean isCollidingWithBlock(Point pt1, Point pt2) { 
//Checks x 
    for(int x = (int) (this.x/Tile.tileSize); x < (int) (this.x/Tile.tileSize + 4); x++) { 
//Checks y 
     for(int y = (int) (this.y/Tile.tileSize); y < (int) (this.y/Tile.tileSize + 4); y++) { 
      if(x >= 0 && y >= 0 && x < Component.dungeon.block.length && y < Component.dungeon.block[0].length) { 
//If the block is not air 
       if(Component.dungeon.block[x][y].id != Tile.air) { 
        //If the player is in contact with point one or two on the block 
        if(Component.dungeon.block[x][y].contains(pt1) || Component.dungeon.block[x][y].contains(pt2)) { 
//Checks for specific blocks 
         if(Component.dungeon.block[x][y].id == Tile.portalBlock) { 
          Component.isLevelDone = true; 
         } 
         if(Component.dungeon.block[x][y].id == Tile.spike) { 
          Health.health -= 1; 
          Component.isJumping = true; 

          if(Health.health == 0) { 
           Component.isDead = true; 
          } 
         } 
         return true; 
        } 
       } 
      } 
     } 
    } 

    return false; 
} 

我在問的是我如何解決這個問題。我已經查看了我的代碼很長一段時間,我不確定它有什麼問題。另外,如果有更有效的方式來做我的碰撞檢查,請讓我知道!

我希望這是足夠的信息,如果它不只是告訴我你需要什麼,我一定會添加它。

謝謝!

+0

你的問題是什麼? –

+0

已添加。對於那個很抱歉。 – Connor

+0

您是否使用過調試器或添加SOP語句來查看所描述的情況下的變量值?這是我建議你開始的地方。學習如何調試代碼就像學習如何編寫代碼一樣重要。 –

回答

0

這個問題可能不是你的碰撞檢查,而是你的碰撞檢查邏輯。你的角色掉進了那個曾經在那裏的街區總是與街區相撞。所以它不能跳躍(因爲當你跳躍的時候我會猜測是否有碰撞)。當你檢查碰撞時,你必須通過預先檢查和調整來確保你的角色不會落入塊中。

if (will collide) { 
    put beside block 
} 

你可能做這樣的事情

if (colliding) { 
    stop moving 
} 

當推杆雖然身邊,你要檢查你正在移動,其方式和你不動成塊。