如何延迟gotoscene

时间:2014-09-07 12:28:06

标签: lua corona

每当特定的食物击中猴子时,游戏重新启动但我想延迟它几秒钟并在重新启动之前显示一些文字,但我似乎无法。它没有延迟,

 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)
            -- get points!
        else
            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
           storyboard.gotoScene("play", "fade", 1000) 
           end

2 个答案:

答案 0 :(得分:2)

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

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

在调用storybaord.gotoScene()

之前会延迟5秒

答案 1 :(得分:2)

像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)
            -- get points!
        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

通过添加变量来检查您是否已经丢失,可以防止它在您处于延迟时运行代码离开。