2014-10-02 150 views
0

儘管我已經閱讀了幾篇關於Lua的教程,並且我在腦海中有一個關於如何使用Lua for循環的想法,但我遇到了麻煩。也許這是因爲我習慣Python的for-loops?每當玩家在我的遊戲中移動時,遊戲就會檢查地圖上是否有牆,並檢查NPC是否在目的地的座標中。Lua的for循環遇到問題(Love2d)

雖然我的牆壁檢查功能完美,但這個新的NPC for-loop可以讓玩家永遠不會移動。 checkSpace函數通過NPC列表(當前只有一個NPC)查看玩家是否可以移動到目的地。

player = { 
    grid_x = 2, 
    grid_y = 2, 
    act_x = 32, 
    act_y = 32, 
    transit = false, 
    direction = {0, 0} 
} 

npc = { 
    grid_x = 4, 
    grid_y = 3, 
    act_x = 64, 
    act_y = 48, 
} 

npcs = {npc} 

function checkSpace(grid_y, grid_x, direction) 
    if map[grid_y + (1 + direction[2])][grid_x + (1 + direction[1])] == 0 then 
     -- if checkNPCs 
     for _,v in pairs(npcs) do 
      if grid_x == v.grid_x and grid_y == v.grid_y then 
       return true 
      end 
     end 
    end 
end 

function love.keypressed(key) 
    if player.transit == false then 
     if key == "up" then 
      player.direction = {0, -1} 
      if checkSpace(player.grid_y, player.grid_x, player.direction) == true then 
       player.grid_y = player.grid_y - 1 
       -- move player.direction before the if statement to make the player change direction whether or no there is space to move 
       player.transit = true 
      end 
     end 
    end 
end 

編輯:我擺弄了一下程序,我取得了一些進展。而不是按鍵程序檢查checkSpace是否返回True,程序已被修改,以便它返回false,如果不是是一種障礙。

if key == "up" then 
     player.direction = {0, -1} 
     if checkSpace(player.grid_y, player.grid_x, player.direction) == false then 
      player.grid_y = player.grid_y - 1 
      -- move player.direction before the if statement to make the player change direction whether or no there is space to move 
      player.transit = true 
     end 

我已經得到了一個非常基本的(和幾乎沒用)for循環與我的程序工作,但如果我嘗試做任何事情更高級的吧,然後我得到的錯誤在我的玩家角色將不會移動。

for nameCount = 1, 1 do 
     if grid_x + direction[1] == npc.grid_x and grid_y + direction[2] == npc.grid_y then 
      return true 
     else 
      return false 
     end 
    end 

我已經發布了完整的代碼在此位置:http://pastebin.com/QNpAU6yi

+0

你是怎麼知道'checkSpace'總是返回'true'? – 2014-10-02 23:06:39

+0

您可以通過牆壁檢查的方向來調整x和y的位置,但不能用於NPC檢查,這是故意的嗎? – Moop 2014-10-03 04:10:46

+0

@Moop我的程序運行的方式是當我嘗試移動播放器時,程序檢查目標廣場是否有牆壁或NPC。如果有牆或NPC,那麼玩家不會移動。 程序的行爲方式,就像每個checkSpace返回true一樣。我拿出腳本的NPC檢查部分,只用牆檢查就可以運行它,它運行良好,所以我猜測的問題是在NPC檢查部分。至於檢查NPC和牆的x和y座標,它是因爲牆的最低X和Y座標是1,而玩家是0。 – 2014-10-03 17:01:28

回答

0

我會假設你的意思是您有這樣的煩惱:

for _,v in pairs(npcs) do 
    if grid_x == v.grid_x and grid_y == v.grid_y then 
     return true 
    end 
end 

這裏的NPC是

npcs = {npc} 

這是一個包含一個項目的表,即npc,它本身是

npc = { 
    grid_x = 4, 
    grid_y = 3, 
    act_x = 64, 
    act_y = 48, 
} 

所以for循環僅涉及一個對(一次迭代),其鍵被丟棄,並且其值是NPC,所以grid_x == v.grid_x and grid_y == v.grid_y比較了給予checkSpace值NPC的格的X和Y,返回true 只有,如果他們匹配。 npcs = {npc} with循環很奇怪,但我會認爲這是因爲你發佈了最簡單的例子。到目前爲止這麼好,沒有錯。

然而,checkSpace玩家的當前位置進行比較,以人大的繞環過來,而你應該檢查是人大是否在建議的目的:

if grid_x + direction[1] == v.grid_x and grid_y + direction[2] == v.grid_y then 

這很難說從你的帖子是否這會解決你的問題,因爲我不認爲你提到的症狀(checkSpace總是返回true)是正確的,但循環是正確的,謂詞是錯誤的,應該像上面顯示的那樣。

+0

我做了改變,但如預期的那樣,主要問題仍然存在。基本上,每當玩家擊中一個方向鍵時,玩家角色就朝這個方向走。如果在途中有牆壁或NPC,玩家角色將不會移動。自從我添加了檢查NPC功能後,問題就出現了,玩家不會移動,這意味着,據我所知,checkSpace函數的NPC部分返回True。 – 2014-10-08 20:45:01