如何延迟gotoscene

每当特定的食物碰到猴子时,游戏就会重新启动,但我想延迟几秒钟并在重新启动之前显示一些文本,但似乎无法实现。它不会延迟。

点赞
用户1870706
用户1870706

为什么不把它放到计时器中:

timer.performWithDelay(5000, function() storyboard.gotoScene("play", "fade", 1000); end)

这将在调用 storybaord.gotoScene() 之前延迟 5 秒钟。

2014-09-08 01:08:03
用户3902590
用户3902590

像 Rob 一样加上一个计时器

timer.performWithDelay(5000, function() storyboard.gotoScene("play", "fade", 1000); end)

但是你现在也有一个问题。如果你已经吃了一个食物之后又吃到了另一个食物怎么办?这将会导致多个计时器触发,还可能会出现故障,因为它将删除已经删除的猴子...

local lostGame = false

local function monkeyCollision( self, event )
    if event.phase == "began" then
        if event.target.type == "monkey" and event.other.type == "food" then

            print( "chomp!" )
            event.other.alpha = 0
            event.other:removeSelf()
            addToScore(5)
            -- 得分!
        else

            if lostGame == false then
                print("ow!")
                monkey.alpha = 0
                monkey:removeSelf()
                displayScore = display.newText( "The total score is " .. score , 0, 0, "Helvetica", 30 )
                displayScore.x = screenLeft +150
                displayScore.y = screenRight-100
                displayre = display.newText( "  The game is going restart", 0, 0, "Helvetica", 25 )
                displayre.x = screenLeft +150
                displayre.y = screenRight-200
                timer.performWithDelay(5000, function() storyboard.gotoScene("play", "fade", 1000); end)

                lostGame = true
            end
        end
    end
end

通过添加一个变量来检查是否已经输了,可以防止在延迟期间运行离开代码。

2014-09-08 19:06:03