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)
}

您正在自己引用 table 键,同时正在分配它。这是 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)