Overide the back button function mulitiple times in an app

In my app, I want multiple uses for the back button, but each time I run only the first one works.

Here is an example of my code from two of the rules screens.

The first:

[lua]

local function onKeyEvent( event )
    local phase = event.phase
    local keyName = event.keyName

     if ( “back” == keyName ) then
         director:changeScene(“home”,“fade”)
         return true
     end
return false
end
Runtime:addEventListener( “key”, onKeyEvent )[/lua]

Then I have this code in another spot:

[lua]

local function onKeyEvent( event )
    local phase = event.phase
    local keyName = event.keyName

    if ( “back” == keyName ) then
        director:changeScene(“rules1”,“fade”)
        return true
    end
return false
end
–add the key callback
Runtime:addEventListener( “key”, onKeyEvent )[/lua]

When I run this on the device, it only uses the first overide. On every screen it returns to the home scene.

I have seen the storyboard examples of creating a memory, but I also have other back button functions in the actual game as well, so that approach will not work.

Are you removing your Runtime listeners when you don’t want them called? I’m presuming your first “back” listener is in tour main.lua file, which usually trumps all other listeners. Again, just an assumption.

Your best bet is to set variables for different states that you want the back button to behave, and then have your listeners listen for those variables.

fwiw: i create a single key listener in main. i then “query” the current scene object and see if I’ve defined a “onBackKeyPress” function in it, and if so I call it - that way each can respond as appropriate without “cluttering” the global logic, or creating lots of local key listeners (that you also have to remember to remove, as per other reply) hth

Why not just have one listener and set a variable as to what scene you want to go to?

Rob

Rob - The reason I can’t set one variable is because during game play I don’t want it to switch scenes.  In the actual game part, when the player presses the back button, I want it to undo their previous turn.  I have the method for that, but I don’t know how to make that work as the back button.

Panc - will that work if the method I want to use is in my other file and not main because it relies on objects in that scene?

pseudo-code, just to convey idea, fill in the blanks:

--in main.lua key listener: local currScene = director/storyboard/composer.getCurrScene()/getCurrentScene()/getScene() if (currScene.onBackKey and type(currScene.onBackKey)=="function") then currScene:onBackKey(event) end -- in your game scene function scene:onBackKey(event) undoPreviousTurn() end -- in some other scene perhaps function scene:onBackKey(event) director/storyboard/composer.gotoScene("theLogicalPreviousScene") -- which need not conform to dir/sb/comp's idea of previous scene end -- in a single how-to-play scene with multiple "pages" function scene:onBackKey(event) -- either: displayPreviousHelpPage() -- or closeHelpAndContinueForwardToPlayScene() -- whichever makes more sense end -- in some popup scene function scene:onBackKey(event) director/storyboard/composer.hideOverlay() end -- perhaps on your title scene function scene:onBackKey(event) if (system.getInfo("platformName")=="Android") then native.requestExit() end end

Thanks Dave! That worked and everything is running fine.

Are you removing your Runtime listeners when you don’t want them called? I’m presuming your first “back” listener is in tour main.lua file, which usually trumps all other listeners. Again, just an assumption.

Your best bet is to set variables for different states that you want the back button to behave, and then have your listeners listen for those variables.

fwiw: i create a single key listener in main. i then “query” the current scene object and see if I’ve defined a “onBackKeyPress” function in it, and if so I call it - that way each can respond as appropriate without “cluttering” the global logic, or creating lots of local key listeners (that you also have to remember to remove, as per other reply) hth

Why not just have one listener and set a variable as to what scene you want to go to?

Rob

Rob - The reason I can’t set one variable is because during game play I don’t want it to switch scenes.  In the actual game part, when the player presses the back button, I want it to undo their previous turn.  I have the method for that, but I don’t know how to make that work as the back button.

Panc - will that work if the method I want to use is in my other file and not main because it relies on objects in that scene?

pseudo-code, just to convey idea, fill in the blanks:

--in main.lua key listener: local currScene = director/storyboard/composer.getCurrScene()/getCurrentScene()/getScene() if (currScene.onBackKey and type(currScene.onBackKey)=="function") then currScene:onBackKey(event) end -- in your game scene function scene:onBackKey(event) undoPreviousTurn() end -- in some other scene perhaps function scene:onBackKey(event) director/storyboard/composer.gotoScene("theLogicalPreviousScene") -- which need not conform to dir/sb/comp's idea of previous scene end -- in a single how-to-play scene with multiple "pages" function scene:onBackKey(event) -- either: displayPreviousHelpPage() -- or closeHelpAndContinueForwardToPlayScene() -- whichever makes more sense end -- in some popup scene function scene:onBackKey(event) director/storyboard/composer.hideOverlay() end -- perhaps on your title scene function scene:onBackKey(event) if (system.getInfo("platformName")=="Android") then native.requestExit() end end

Thanks Dave! That worked and everything is running fine.