Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 数组中的对象有助于Lua?_Arrays_Oop_Lua - Fatal编程技术网

Arrays 数组中的对象有助于Lua?

Arrays 数组中的对象有助于Lua?,arrays,oop,lua,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()行):尝试索引字

所以,我有一个数组

//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()行):尝试索引字段“?”(一个空值)。。。但是它里面有东西!对吧?

有人试过这样的东西吗?有人能修好吗

谢谢


Nate

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

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

for i, mob in ipairs(Mobs) do
  mob:draw()
end
哦,千万不要使用
getn