BackBtn不会从前一个场景取消计时器

时间:2015-11-01 14:13:16

标签: timer lua corona back-button cancellation

我无法找到正确的代码来取消之前场景中的计时器。我在不同的位置试过这些组合:

      if "tap" == true then
           timer.cancel()
      end

           timer.cancel(event.source)

      timer.cancel = true

      timer.stop

    if "tap" == true then
            timer.cancel = true
            timer.stop()
    end

......等等。有人能指出我正确的代码以及准确放置的位置吗?谢谢。 这些是有问题的场景:

- info.lua

local composer = require "composer"
local scene = composer.newScene()

function scene:create(event)
    local sceneGroup = self.view

    local widget = require "widget"
    local scene1Btn
    local function onScene1BtnRelease()
            composer.gotoScene("scene1", "fade", 40)
            return true 
    end

    scene1Btn = widget.newButton{
            defaultFile = "images/reviewTwo.png",
            width= 480, height= 320,
            onRelease = onScene1BtnRelease 
    }

    sceneGroup:insert(scene1Btn)
end

scene:addEventListener("create", scene)

return scene

- scene1.lua

    local myData = require ("myData")

    local composer = require ( "composer")
    local scene = composer.newScene()

            local function showScene2()
                    local options = {
                            effect = "slideLeft",
                            time = 130,
                    }
    composer.gotoScene("scene2", options)
    end

   -- create scene 

    function scene:createScene ( event )
    local sceneGroup = self.view
    end     

    function scene:show( event )
    local sceneGroup = self.view
    local phase = event.phase

-- will                                                                     

    if ( phase == "will") then
            data = data + 1
                    if data == 5 then
                    sub = sub + 1
                    data = 1
            end
            if sub == 3 then
            section = section + 1
                    sub = 1
                    data = 1
            end

            background = display.newImage( "images/staveBlankgrey2.png", 240, 160 )
            note1 = display.newImage( "images/crDown.png", 130, 141 )
            count = display.newImage( workoutTable[section][sub][data], 60, 40 ) 

            sceneGroup:insert( background )
            sceneGroup:insert( note1 )
            sceneGroup:insert( count )


-- goBackBtn                                                                                

            local object = display.newImage("images/goBackBtn.png", 240, 250)
            object.name = "button object"

            local function onObjectTap( self, event )
                    composer.gotoScene( "info" )
                    return true
            end
            object.tap = onObjectTap
            object:addEventListener( "tap", object )
            sceneGroup:insert( object )

-- did                                                                                                                 

     elseif ( phase == "did") then
            local function showScene2()
                 local options = {
                    effect = "slideLeft",
                    time = 30,
                 }
            composer.gotoScene( "scene2", options )
            end

            timer.performWithDelay( tempo, showScene2 )
            end
     end     

-- hide / destroy

    function scene:hide( event )
            local sceneGroup = self.view
            local phase = event.phase
    end

    function scene:destroy( event )
            local sceneGroup = self.view
    end

    scene:addEventListener( "create", scene )
    scene:addEventListener( "show", scene )
    scene:addEventListener( "hide", scene )
    scene:addEventListener( "destroy", scene )

    return scene

- scene2.lua

    local composer = require ( "composer")
    local scene = composer.newScene()

            local function showScene1()
                    local options = {
                            effect = "slideLeft",
                            time = 130,
                    }
            composer.gotoScene("scene1", options)
            end

-- create scene 

    function scene:createScene ( event )
    local sceneGroup = self.view
    end     

    function scene:show( event )
    local sceneGroup = self.view
    local phase = event.phase

-- will

    if ( phase == "will") then
     background = display.newImage( "images/staveBlankgrey2.png", 240, 160 )
     note1 = display.newImage( "images/mmDown.png", 130, 172 )

    sceneGroup:insert( background )
    sceneGroup:insert( note1 )


-- goBackBtn 

   local object = display.newImage("images/goBackBtn.png", 240, 250)
       object.name = "button object"

       local function onObjectTap( self, event )
            composer.gotoScene( "info" )
            return true
       end
   object.tap = onObjectTap
   object:addEventListener( "tap", object )
                    sceneGroup:insert( object )

-- did

   elseif ( phase == "did") then
       local function showScene1()
           local options = {
                effect = "slideLeft",
                time = 30,
                                    }
       composer.gotoScene( "scene1", options )
       end

       timer.performWithDelay(tempo, showScene1 )
     end 
    end     


-- hide / destroy

    function scene:hide( event )
            local sceneGroup = self.view
            local phase = event.phase
    end

    function scene:destroy( event )
            local sceneGroup = self.view
    end

    scene:addEventListener( "create", scene )
    scene:addEventListener( "show", scene )
    scene:addEventListener( "hide", scene )
    scene:addEventListener( "destroy", scene )

    return scene

1 个答案:

答案 0 :(得分:0)

正如我所说,你想在去另一个场景时取消计时器。只需使用对计时器的引用:

local myTimer1 = timer.performWithDelay(tempo, showScene1 )

然后在场景中取消它:隐藏或场景:毁灭或你想要的任何其他地方。

if myTimer1 then 
    timer.cancel(myTimer1)
    myTimer1 = nil
end

当然,您必须在您可以访问它的地方声明计时器变量(myTimer1)。例如,主要范围