0

I am using Marmalade Quick.

I can draw a rectangle with:

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

Is there a way to store the myRectangle variable in an array for later use? Or how can I make multiple rectangles and have access to each of them?

4

2 回答 2

1

是的,只需使用 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-04T16:37:34.470 回答
0

是的,您可以创建一个表

myRectangles = {}

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

myRectangles[#myRectangles+1] = director:createRectangle(x1, y1, width1, height1)
myRectangles[#myRectangles+1] = director:createRectangle(x2, y2, width2, height2)
于 2013-06-04T16:38:12.833 回答