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?

有帮助吗?

解决方案

yeah, just use lua tables.

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

now, if you want to examine all of your rectangles you can just iterate over rects.

if you absolutely have to store all of your references to rectangles, i'd suggest making a helper method to automate that part for you, something like this maybe:

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

and then you could just call your helper function and know that each rectangle object it returns to you has automatically been added to your list for later.

local myRect = createRect(1, 1, 1, 1)

其他提示

Yes, you can create a table

myRectangles = {}

and add the rectangles on to the end of the table as they are created.

myRectangles[#myRectangles+1] = director:createRectangle(x1, y1, width1, height1)
myRectangles[#myRectangles+1] = director:createRectangle(x2, y2, width2, height2)
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top