簡體   English   中英

map-functions.lua:60:嘗試索引一個 nil 值(love2d,物理)

[英]map-functions.lua:60: attempt to index a nil value (love2d, physics)

我知道錯誤是由索引不存在引起的,但我不知道為什么它不存在。 我正在嘗試在 mapDraw 方法中實現一個程序,該程序向每個牆磚(#)添加一個物理對象:

function drawMap()
  objects = {}
  for x,column in ipairs(TileTable) do
    for y,char in ipairs(column) do
      love.graphics.draw(Tileset, Quads[ char ] , (x-1)*TileW, (y-1)*TileH)
      if char == '#' then --addding the physics for collision(walls)--
        objects[objectIndex] = {
          body = love.physics.newBody(world, (x-1/2) * TileW, (x-1/2) * TileH),
          shape = love.physics.newRectangleShape(32, 32),
          fixture = love.physics.newFixture(objects[objectIndex].body, objects[objectIndex].shape, 1)
        }
      end
    end
  end
end

我只是從 love2d 和游戲制作開始,希望得到幫助,謝謝。

在以下代碼段中:

objects[objectIndex] = {
  body = love.physics.newBody(world, (x-1/2) * TileW, (x-1/2) * TileH),
  shape = love.physics.newRectangleShape(32, 32),
  fixture = love.physics.newFixture(objects[objectIndex].body, objects[objectIndex].shape, 1)
}

在分配表鍵時,您正在自引用表鍵。 這是 lua 中的無效步驟。 稍后為fixture鍵分配一個值:

objects[objectIndex] = {
  body = love.physics.newBody(world, (x-1/2) * TileW, (x-1/2) * TileH),
  shape = love.physics.newRectangleShape(32, 32)
}
objects[objectIndex].fixture = love.physics.newFixture(objects[objectIndex].body, objects[objectIndex].shape, 1)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM