数组中的对象帮助Lua?

时间:2012-08-25 15:28:14

标签: arrays oop lua

所以,我有一个数组

//loop here
nummobs = nummobs + 1
Mobs = {}
Mobs[nummobs] = Entity.Init(x(locations to spawn mob), y(locations to spawn mob),"testMob")

然后,调用draw方法......

for i = 0, table.getn(Mobs) do
    Mobs[i].draw()
end

错误:map.lua:54(Mobs [i] .draw()行):尝试索引字段'?' (零价值)......但它有一些东西!正确?

有人试过这样的事吗?任何人都可以解决它吗?

由于

内特

1 个答案:

答案 0 :(得分:2)

Lua对数组使用基于1的索引。因此,数组的范围为[1, n],其中n是元素的数量。

更重要的是,您可以使用ipairs而不必写出循环组件:

for i, mob in ipairs(Mobs) do
  mob:draw()
end

哦,永远不要使用getn; use the # length operator instead