后退按钮不会导航到所需的场景

时间:2015-10-05 14:35:30

标签: lua corona back-button

在“tap”上,firstBar1.lua转到scene1.lua。如果在场景1超时期间没有敲击,则它会移动到场景2,依此类推; scene1有一个指向firstBar1的goBack按钮。它会回来一会然后继续到现场2.它需要留在firstBar1,直到有另一个水龙头。我尝试了各种按钮代码,这是我能够获得的最佳结果。点击后退按钮后,print(“firstBar1”)不起作用。

    -- firstBar1.lua
    -----------------------------------------------------

    local composer = require ("composer")            
            print ("firstBar1")
    local scene = composer.newScene()                                       
    function scene:create(event) 
            local screenGroup = self.view

    local widget = require ("widget")

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

    image1 = "images/staveBlankgrey2.png" 

    local scene1Btn = widget.newButton{
            defaultFile = image1,
            width = 480, height = 320,
            onRelease = onScene1BtnRelease
    } 
        screenGroup:insert(scene1Btn) 

    end

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

  return scene

场景1:

    -- 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 3

    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

        stave = 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( stave )
            sceneGroup:insert( note1 )
            sceneGroup:insert( count )

        -- goBackBtn                                   

            local widget = require ("widget")

            local button = display.newImage("images/goBackBtn.png")
            button.x = display.contentWidth / 2
            button.y = display.contentHeight - 60

            sceneGroup:insert( button )

            function button:tap(event)
                    composer.gotoScene("firstBar1", "fade", 30)
            end
            button:addEventListener("tap", button)
            print ("button 1 pressed")
            sceneGroup:insert( button )
            return true

        -- 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

如果我遗失了某些东西,你还可以告诉我在哪里加入它吗?谢谢。

1 个答案:

答案 0 :(得分:0)

当您处理后退按钮触摸时,您需要取消进入下一个场景的计时器。

在模块的顶部声明一个变量来存储定时器的句柄:

local sessionTimer

然后当你启动计时器时:

sessionTimer = timer.performWithDelay(tempo, showScene2 )

最后,如果您在计时器退出之前离开场景,可能在您的按钮处理程序中,请执行以下操作:

timer.cancel( sessionTimer )

或类似的东西。