我目前的代码是:
obj1 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
obj2 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
obj3 = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
等等。因为它们都是一样的,所以我希望能够生成一个变量,我可以将名称增加一个,然后使用for循环来创建它们。我有办法在Lua做到这一点吗?谢谢!
答案 0 :(得分:4)
您可以使用表格来保存对象,并使用for循环添加它们。
local objects = {}
--> This will create 20 objects
for i=1, 20 do
objects[i] = object:new{x = math.random(1,92), y = math.random(1,92), roomx = 0, roomy = 0, symbol = "t", name = "Tree"}
end