2012-10-24 46 views
0

我正在製作一個遊戲,有一個角色在迷宮中移動。運動從一個方塊的中心到下一個方向的中心,在每個運動方向上選擇(北,南,東或西)方向,但我在牆上遇到了一些問題。尋找鄰近的瓷磚是否在路徑上

我現在編碼的方式目前工作正常,但非常冗長,我擔心這可能會導致未來的問題。基本上我會搜索玩家在舞臺上可能會出現的每個區塊,然後確定玩家對於移動方向有哪些選項。例如:

if (player.center.x = tile_001.x && player.center.y = tile_001.y){ 
    player has option to move in east and west directions 
} 

if (player.center.x = tile_002.x && player.center.y = tile_002.y){ 
    player has option to move in east, north and south directions 
} 

這意味着我有超過100 if語句來確定一個角色可以移動了,我知道是一個更簡單的方法。我想要找到一種方法來搜索玩家當前位置的四個相鄰區塊(無論哪裏可能在迷宮中)並確定它們是在路徑上還是屬於牆壁的一部分。

我知道很多關於堆棧溢出的問題通常會有更多的代碼已經寫入其中,但我現在還沒有任何想法如何做到這一點。任何幫助將不勝感激。 :)

回答

0

這理論不是具體的Xcode,因爲我不知道的語言,所以我已經離開了應將描述一般,足以在任何通俗的語言實現的:

讓「瓦」是結構/對象其限定:

  • 的圖形
  • 類型,如果瓦片塊運動

讓板'是多維數組或數組的數組。

假設有分配的方向/箭頭鍵處理程序:goNorth(),goEast(),goWest(),和goWest():

goNorth(player){ 
    int newX = player.x + 1; 
    if(isPassable(newX ,y) == true) 
    player.x = newX; 
    else 
    //set error condition to be used when updating board to make beep noise 
    //redraw the board 
}; 

goEast(player){ 
    int newY = player.y + 1; 
    if(isPassable(x,newY) == true) 
    player.y = newY; 
    else 
    //set error condition to be used when updating board to make beep noise 
    //redraw the board 
} 

goSouth(player){ 
    int newX = player.x - 1; 
    if(isPassable(newX,y) == true) 
    player.x = newX ; 
    else 
    //set error condition to be used when updating board to make beep noise 
    //redraw the board 
} 

goWest(player){ 
    int newY = player.y - 1; 
    if(isPassable(x,newY) == true) 
    player.y = newY; 
    else 
    //set error condition to be used when updating board to make beep noise 
    //redraw the board 
} 

bool isPassable(x,y){ 
bool passable = false; 
//check bounds, if it is outside of range we will return false to prevent the movement 
if((x > -1 && x <= maxWidthIndex) && (y > -1 && y <= maxHeightIndex)) 
    passable = (board[x][y].passable == true); 
return passable; 
}