我无法弄清楚如何将一个简单函数创建的对象放在表中,让它们成为单独的身份。
E.g。
local function spawncibo()
nuovoCibo = display.newImage("immagini/cibo/cibo001.png")
end
timer.performWithDelay(1500, spawncibo, -1)
我尝试用for循环来做,但它不起作用(如果我尝试打印表格,我总是得到一个零结果)。
任何建议都会非常感激!
答案 0 :(得分: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)
这将每1.5秒读取文件cibo001.png
,cibo002.png
,...并将所有图片放入cibo
数组。
答案 1 :(得分: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
答案 2 :(得分: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) --
如果您有任何其他问题,请随时提出。