2015-03-19 69 views
0

所以我想通過一個循環將我的硬幣對象添加到我的gameGroup中,而不是逐行添加每個對象 現在我想要做的事明顯不會因爲你不能添加字符串到對象組將對象添加到Lua中的循環組中

有沒有辦法做到這一點?

local coin1 = getCoin(6000,500) 
local coin2 = getCoin(850,550) 
local coin3 = getCoin(980,550) 
local coin4 = getCoin(1900,550) 
local coin5 = getCoin(2850,380) 
local coin6 = getCoin(3199,486) 
local coin7 = getCoin(3543,455) 
local coin8 = getCoin(3780,380)  
local coinOverlay1 = display.newRect(850,550,80,80) 
physics.addBody(coinOverlay1, { bounce=0 }) 
coinOverlay1.isVisible = false 
gameGroup:insert(coinOverlay1) 
sceneGroup:insert(coinGroup) 
coinGroup:insert(coin1) -- Not this would take up too many lines for each coin,there will be over 40 coins 
coinGroup:insert(coin2) 

camera:add(coinGroup, 1) 
for i=1,8 do 
    local test_1 = "coin" 
    local test_2 = i 
    local test_3 = "coin"..test_2 
    print (test_3) 
    coinGroup:insert(test_3) 

end 
+0

使用一個表來保存所有的硬幣,並在表上循環。 – 2015-03-19 01:44:37

回答

0

這樣的事情?

coins = { 
    {6000,500}, 
    {850,550}, 
    {980,550}, 
    {1900,550}, 
    {2850,380}, 
    {3199,486}, 
    {3543,455}, 
    {3780,380} 
} 

for i,v in ipairs(coins) do 
    coinGroup:insert(getCoin(v[1],v[2])) 
end 
+0

非常感謝! – Alurus 2015-03-19 18:48:21

相關問題