在Lua中将对象添加到循环组中

时间:2015-03-19 01:00:34

标签: lua

所以我想通过循环将我的硬币对象添加到我的gameGroup,而不是逐行手动添加每个对象 我现在尝试做的事情显然不起作用,因为你无法在一组对象中添加一个字符串

有办法做到这一点吗?

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

1 个答案:

答案 0 :(得分: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