2013-06-04 30 views
0

我正在使用Marmalade Quick。有沒有辦法制作矩形數組?

我能畫一個矩形:

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

是否有可以myRectangle變量保存在供以後使用數組的方法嗎?或者我如何製作多個矩形並訪問每個矩形?

回答

1

是的,只是使用盧阿表。

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) 
+0

謝謝!我嘗試過,但我語法混亂。 – Nikmaster

0

是的,你可以創建一個表

myRectangles = {} 

,併爲他們在創建上添加矩形到表的末尾。

myRectangles[#myRectangles+1] = director:createRectangle(x1, y1, width1, height1) 
myRectangles[#myRectangles+1] = director:createRectangle(x2, y2, width2, height2) 
相關問題