2

我目前的代码是:

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 中做到这一点?谢谢!

4

1 回答 1

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
于 2013-05-03T00:52:10.087 回答