如何在 Corona SDK 程序中解释重启后的故障转换

How to explain glitchy transitions after restart in Corona SDK program

下面是一个简单的Lua架子在屏幕上左右移动的程序。当点击屏幕时,架子应该会重置并再次从左侧开始。 但在那之后,transitions.to 无法正常工作,因为架子开始来回 "jumping"。 有人可以帮我解决这个问题吗?

local shelf

local function movingShelf() 
    shelf = display.newRect(56.5,250,85,10)  --display shelf

    local moveLeft --move shelf
    local function moveRight()     
        transition.to(shelf, {time = 5000, x = 261, onComplete = moveLeft})
    end
    moveLeft = function()      
        transition.to(shelf, {time = 5000, x = 57.5, onComplete = moveRight})
    end
    moveRight()
end

local function restart() -- restart the program
    shelf:removeSelf()
    movingShelf()
end

movingShelf()
Runtime:addEventListener("tap", restart) -- tap to restart   

您在 moveLeft()moveRight() 中启动的转换应用于 shelf,并且您没有取消它们。这可能会导致这种故障行为。

要正确删除 DisplayObject,您需要取消它所涉及的任何转换。在您的 restart 函数中,您应该在使用 shelf: removeSelf() 删除 DisplayObject 之前执行 transition.cancel( shelf )。最后,添加 shelf=nil 作为衡量标准(在 Corona 中删除 DisplayObjects 时推荐这样做以避免内存泄漏)。