完成过渡后移除对象。

我正在创造一些矩形,每个矩形都会过渡并向屏幕下移动。我试图从内存中删除那些完成过渡的矩形(即向下移动且不再可见的矩形)。但是实际上删除了可见的(新生成的)矩形。

这是我的代码

-- 用于存储动态创建的矩形的表
local rects = {}
-- 用作表索引
local numRect = 0

-- 用于删除矩形的函数
local function removeRect(obj)
    local rectid = obj.id

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

-- 用于生成矩形的函数
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)
点赞
用户2285255
用户2285255

onComplete监听器已经接收到了正在转换的对象,所以你不需要传递它。

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

transition.to(rects[numRect],{time = 9000, y = display.contentHeight + 100,onComplete = removeRect})
2014-07-05 00:12:39