有没有办法创建一个矩形数组?

我正在使用Marmalade Quick。

我可以用以下代码绘制一个矩形:

local myRectangle = director:createRectangle(x, y, width, height)

有没有办法将 myRectangle 变量存储在数组中以供以后使用? 或者我该如何绘制多个矩形并访问每个矩形?

点赞
用户1208078
用户1208078

是的,只需使用 lua 表格。

local rects = {}
local myRect = director:createRectangle(x, y, width, height)
table.insert(rects, myRect)

现在,如果你想检查所有的矩形,你可以迭代 rects

如果你绝对必须存储所有的矩形引用,我建议你创建一个帮助方法来自动化这部分,例如:

local rects = {}
function createRect(x, y, width, height)
    local rect = director:createRectangle(x, y, width, height);
    table.insert(rects, rect)
    return rect
end

然后你可以调用你的帮助函数并知道它返回给你的每个矩形对象都已经自动添加到你的列表中以备后用。

local myRect = createRect(1, 1, 1, 1)
2013-06-04 16:37:34
用户622310
用户622310

是的,你可以创建一个表格

myRectangles = {}

在创建矩形的同时将它们添加到表格末尾。

myRectangles[#myRectangles+1] = director:createRectangle(x1, y1, width1, height1)
myRectangles[#myRectangles+1] = director:createRectangle(x2, y2, width2, height2)
2013-06-04 16:38:12