2012-09-11 37 views
1

我无法弄清楚如何把一个简单的函数创建的对象在一个表中,有他们的身影如个人身份..科罗娜SDK:把项目通过创建中的函数表

例如

local function spawncibo() 
nuovoCibo = display.newImage("immagini/cibo/cibo001.png") 
end 
timer.performWithDelay(1500, spawncibo, -1) 

我试图用一个做for循环,但它不工作(如果我尝试打印表格,我总是得到一个无结果)。

任何建议将非常感激!

回答

0

提供我理解正确你的问题,你可以尝试这样的事:

local cibo = {} 
local function spawncibo() 
    cibo[#cibo+1] = display.newImage(string.format(
    "immagini/cibo/cibo%3d.png", #cibo+1)) 
end 
timer.performWithDelay(1500, spawncibo, -1) 

这将读取文件cibo001.pngcibo002.png,每1.5秒......并把所有图像导入cibo阵列。

0
local spawnedCibos = {} 
local function spawncibo() 
    nuovoCibo = display.newImage("immagini/cibo/cibo001.png") 
    table.insert(spawnedCibos, nuovoCibo); 
end 
timer.performWithDelay(1500, spawncibo, -1); 

local function enterFrameListener(event) 
    for index=#spawnedCibos, 1, -1 do 
     local cibo = spawnedCibos[index]; 
     cibo.x = cibo.x + 1; 
     if cibo.x > display.contentWidth then 
      cibo:removeSelf(); 
      table.remove(spawnedCibos, index); 
     end 
    end 
end 
0

你可以试试这个...

local spawned = {} -- local table to hold all the spawned images 
local timerHandle = nil -- local handle for the timer. It can later be used to cancel it if you want to 

local function spawnCibo() 
    local nuovoCibo = display.newImage('immagini/cibo/cibo001.png') 
    table.insert(spawned, nuovoCibo) -- insert the new DisplayObject (neovoCibo) at the end of the spawned table. 
end 

local function frameListener() 
    for k, v in pairs(spawned) do -- iterate through all key, value pairs in the table 
     if (conditions) then -- you will probably want to change conditions to some sort of method to determine if you want to delete the cibo 
      display.remove(spawned[k]) -- remove the part of the object that gets rendered 
      spawned[k] = nil -- remove the reference to the object in the table, freeing it up for garbage collection 
     end 
    end 
end 

timer.performWithDelay(1500, spawnCibo, 0) -- call spawnCibo() every 1.5 seconds, forever (That's what the 0 is for) or until timer.cancel is called on timerHandle 
Runtime:addEventListener('enterFrame', frameListener) -- 

如果您有任何疑问,请随时问。