在转换完成时删除对象

时间:2014-07-04 21:36:13

标签: lua corona

我正在产生一些矩形,每个矩形都会转换为在屏幕上向下移动。我试图从事务完成的内存中删除那些矩形(意味着矩形向下移动到屏幕上并且在屏幕上不再可见)。但相反,可见(新生成的)矩形被移除。

这是我的代码

--table to hold dynamically created rectangles
local rects={}
--used as index for table
local numRect=0

local function removeRect( obj )
    local rectid = obj.id

    obj:removeSelf( )
    rects[rectid] = nil
end

--function for spawning rectangles
local function spawnRect()
    numRect = numRect + 1
    rects[numRect] = display.newRect( display.contentWidth/2, 100, 100, 100)
    rects[numRect]:setFillColor( 1,1,1 )
    rects[numRect].id = numRect

    transition.to( rects[numRect], {time = 9000, y = display.contentHeight + 100,
            onComplete = function()
                removeRect(rects[numRect])
            end

    } )
end

timer.performWithDelay( 1000, spawnRect, -1 )

1 个答案:

答案 0 :(得分:2)

onComplete侦听器已经接收了正在转换的对象。所以,你不需要通过它。

只需将您的transition.to更改为onComplete = removeRect,如下所示:

transition.to( rects[numRect], {time = 9000, y = display.contentHeight + 100,
        onComplete = removeRect } )
相关问题