简体   繁体   English

map-functions.lua:60:尝试索引一个 nil 值(love2d,物理)

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

I know that the error is caused by the index not existing, but i dont know why it is not existing.我知道错误是由索引不存在引起的,但我不知道为什么它不存在。 I am trying to make a program implemented in the mapDraw method which adds to every wall tile(#) a physics object:我正在尝试在 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

I am only starting out with love2d and game making and would appriciate help, thank you.我只是从 love2d 和游戏制作开始,希望得到帮助,谢谢。

In the following snippet:在以下代码段中:

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

you are self referencing the table key, while it is being assigned.在分配表键时,您正在自引用表键。 This is an invalid step in lua.这是 lua 中的无效步骤。 Assign the fixture key a value later:稍后为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