I tried following this postand have been unable to get the Back button to work in either the simulator or on my phone. I used the code referred to on this blogand tried to print out on Key Event… but nothing happens. What can I be doing wrong?
I tried putting the onKeyEvent function in main.lua and other lua files, but nothing happens (I do add the event listeners)
Here is the code… At launch, the main.lua file just fires up the database and goes to splash screen:
MAIN.LUA
display.setStatusBar(display.HiddenStatusBar) ------------------------ -- Managging Database -- ------------------------ require "sqlite3" local data\_path=system.pathForFile("data.db",system.DocumentsDirectory); db=sqlite3.open(data\_path); local sql= "CREATE TABLE IF NOT EXISTS settings (name,value);" db:exec(sql); function setSetting(name,value) sql="DELETE FROM settings WHERE name='"..name.."'"; db:exec( sql ) sql="INSERT INTO settings (name,value) VALUES ('"..name.."',"..value..");"; db:exec( sql ) end function getSetting(name) local sql="SELECT \* FROM settings WHERE name='"..name.."'"; local value=-1; for row in db:nrows(sql) do value=row.value; end return value; end local storyboard = require "storyboard" storyboard.gotoScene("splash", "fade")
The Splash screen just shows the image and moves onto Menu.lua
splash.lua
local storyboard = require "storyboard" local scene = storyboard.newScene() local scaleX = display.contentWidth / 480 local scaleY = display.contentHeight/ 860 local background function scene:createScene(event) local screenGroup = self.view -- Background -- background = display.newImage ("splashscreen.png") background.x = display.contentWidth/2 background.y = display.contentHeight/2 background.xScale = scaleX background.yScale = scaleY screenGroup:insert(background) end function LoadMenu() storyboard.gotoScene("menu", "fade", 500) end function scene:enterScene(event) end function scene:exitScene(event) end function scene:destroyScene(event) end timer.performWithDelay(2000, LoadMenu, 1) scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene
Menu.lua
I have defined the function to handle the hardware button right at the end
local storyboard = require "storyboard" local gamestate = require( "gamestate" ) local scene = storyboard.newScene() local background, playButton, aboutButton, exitButton local scaleX = display.contentWidth / 480 local scaleY = display.contentHeight/ 860 function scene:createScene(event) local music = audio.loadStream("music.mp3") audio.play(music, { channel=1, loops=-1 }) local screenGroup = self.view -- Background -- background = display.newImage ("menubackground.png") background.x = display.contentWidth/2 background.y = display.contentHeight/2 background.xScale = background.xScale \* scaleX background.yScale = background.yScale \* scaleY screenGroup:insert(background) -- Play Button -- playButton = display.newImage ("menuImages/Play.png") playButton.x = display.contentWidth/2 playButton.y = display.contentHeight \* 1/8 playButton.xScale = playButton.xScale \* scaleX playButton.yScale = playButton.yScale \* scaleY screenGroup:insert(playButton) -- About Button -- aboutButton = display.newImage ("menuImages/About.png") aboutButton.x = display.contentWidth/2 aboutButton.y = display.contentHeight \* 3/8 aboutButton.xScale = aboutButton.xScale \* scaleX aboutButton.yScale = aboutButton.yScale \* scaleY screenGroup:insert(aboutButton) -- Tutorial Button -- tutorialButton = display.newImage ("menuImages/Tutorial.png") tutorialButton.x = display.contentWidth/2 tutorialButton.y = display.contentHeight \* 5/8 tutorialButton.xScale = tutorialButton.xScale \* scaleX tutorialButton.yScale = tutorialButton.yScale \* scaleY screenGroup:insert(tutorialButton) -- Exit Button -- exitButton = display.newImage ("menuImages/Exit.png") exitButton.x = display.contentWidth/2 exitButton.y = display.contentHeight \* 7/8 exitButton.xScale = exitButton.xScale \* scaleX exitButton.yScale = exitButton.yScale \* scaleY screenGroup:insert(exitButton) --\> ------------------------ \<-- end local function pressPlay (event) if event.phase == "began" then storyboard.gotoScene("stages", "fade", 500) end end local function pressAbout (event) if event.phase == "began" then storyboard.gotoScene("about", "fade", 500) end end local function pressTutorial (event) if event.phase == "began" then storyboard.gotoScene("howtoplay", "fade", 500) end end local function pressExit (event) if event.phase == "began" then os.exit() end end function scene:enterScene(event) local prior\_scene = storyboard.getPrevious() storyboard.purgeScene( prior\_scene ) storyboard.removeAll() -- if (gamestate.level \< 10) then -- storyboard.purgeScene("level0" .. gamestate.level) -- end -- if (gamestate.level \>= 10) then -- storyboard.purgeScene("level" .. gamestate.level) -- end playButton:addEventListener ("touch", pressPlay) aboutButton:addEventListener ("touch", pressAbout) tutorialButton:addEventListener ("touch", pressTutorial) exitButton:addEventListener ("touch", pressExit) end function scene:exitScene(event) playButton:removeEventListener ("touch", pressPlay) aboutButton:removeEventListener ("touch", pressAbout) tutorialButton:removeEventListener ("touch", pressTutorial) exitButton:removeEventListener ("touch", pressExit) end function scene:destroyScene(event) local screenGroup = self.view end local function onKeyEvent( event ) print("event happened") local phase = event.phase local keyName = event.keyName if ( "back" == keyName and phase == "down" ) then native.requestExit() end if ( keyName == "volumeUp" and phase == "down" ) then local masterVolume = audio.getVolume() if ( masterVolume \< 1.0 ) then masterVolume = masterVolume + 0.1 audio.setVolume( masterVolume ) end return true elseif ( keyName == "volumeDown" and phase == "down" ) then local masterVolume = audio.getVolume() if ( masterVolume \> 0.0 ) then masterVolume = masterVolume - 0.1 audio.setVolume( masterVolume ) end return true end return false --SEE NOTE BELOW end --add the key callback Runtime:addEventListener( "key", onKeyEvent ) scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene
Personally I would put the key handling in your main.lua before you call storyboard.gotoScene(). The code you have looks right. The way you have it written, your app should exit when the back button is pressed.
Also, you should put your 2 second timer inside the the splash.lua’s enterScene() function.
Here is the code… At launch, the main.lua file just fires up the database and goes to splash screen:
MAIN.LUA
display.setStatusBar(display.HiddenStatusBar) ------------------------ -- Managging Database -- ------------------------ require "sqlite3" local data\_path=system.pathForFile("data.db",system.DocumentsDirectory); db=sqlite3.open(data\_path); local sql= "CREATE TABLE IF NOT EXISTS settings (name,value);" db:exec(sql); function setSetting(name,value) sql="DELETE FROM settings WHERE name='"..name.."'"; db:exec( sql ) sql="INSERT INTO settings (name,value) VALUES ('"..name.."',"..value..");"; db:exec( sql ) end function getSetting(name) local sql="SELECT \* FROM settings WHERE name='"..name.."'"; local value=-1; for row in db:nrows(sql) do value=row.value; end return value; end local storyboard = require "storyboard" storyboard.gotoScene("splash", "fade")
The Splash screen just shows the image and moves onto Menu.lua
splash.lua
local storyboard = require "storyboard" local scene = storyboard.newScene() local scaleX = display.contentWidth / 480 local scaleY = display.contentHeight/ 860 local background function scene:createScene(event) local screenGroup = self.view -- Background -- background = display.newImage ("splashscreen.png") background.x = display.contentWidth/2 background.y = display.contentHeight/2 background.xScale = scaleX background.yScale = scaleY screenGroup:insert(background) end function LoadMenu() storyboard.gotoScene("menu", "fade", 500) end function scene:enterScene(event) end function scene:exitScene(event) end function scene:destroyScene(event) end timer.performWithDelay(2000, LoadMenu, 1) scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene
Menu.lua
I have defined the function to handle the hardware button right at the end
local storyboard = require "storyboard" local gamestate = require( "gamestate" ) local scene = storyboard.newScene() local background, playButton, aboutButton, exitButton local scaleX = display.contentWidth / 480 local scaleY = display.contentHeight/ 860 function scene:createScene(event) local music = audio.loadStream("music.mp3") audio.play(music, { channel=1, loops=-1 }) local screenGroup = self.view -- Background -- background = display.newImage ("menubackground.png") background.x = display.contentWidth/2 background.y = display.contentHeight/2 background.xScale = background.xScale \* scaleX background.yScale = background.yScale \* scaleY screenGroup:insert(background) -- Play Button -- playButton = display.newImage ("menuImages/Play.png") playButton.x = display.contentWidth/2 playButton.y = display.contentHeight \* 1/8 playButton.xScale = playButton.xScale \* scaleX playButton.yScale = playButton.yScale \* scaleY screenGroup:insert(playButton) -- About Button -- aboutButton = display.newImage ("menuImages/About.png") aboutButton.x = display.contentWidth/2 aboutButton.y = display.contentHeight \* 3/8 aboutButton.xScale = aboutButton.xScale \* scaleX aboutButton.yScale = aboutButton.yScale \* scaleY screenGroup:insert(aboutButton) -- Tutorial Button -- tutorialButton = display.newImage ("menuImages/Tutorial.png") tutorialButton.x = display.contentWidth/2 tutorialButton.y = display.contentHeight \* 5/8 tutorialButton.xScale = tutorialButton.xScale \* scaleX tutorialButton.yScale = tutorialButton.yScale \* scaleY screenGroup:insert(tutorialButton) -- Exit Button -- exitButton = display.newImage ("menuImages/Exit.png") exitButton.x = display.contentWidth/2 exitButton.y = display.contentHeight \* 7/8 exitButton.xScale = exitButton.xScale \* scaleX exitButton.yScale = exitButton.yScale \* scaleY screenGroup:insert(exitButton) --\> ------------------------ \<-- end local function pressPlay (event) if event.phase == "began" then storyboard.gotoScene("stages", "fade", 500) end end local function pressAbout (event) if event.phase == "began" then storyboard.gotoScene("about", "fade", 500) end end local function pressTutorial (event) if event.phase == "began" then storyboard.gotoScene("howtoplay", "fade", 500) end end local function pressExit (event) if event.phase == "began" then os.exit() end end function scene:enterScene(event) local prior\_scene = storyboard.getPrevious() storyboard.purgeScene( prior\_scene ) storyboard.removeAll() -- if (gamestate.level \< 10) then -- storyboard.purgeScene("level0" .. gamestate.level) -- end -- if (gamestate.level \>= 10) then -- storyboard.purgeScene("level" .. gamestate.level) -- end playButton:addEventListener ("touch", pressPlay) aboutButton:addEventListener ("touch", pressAbout) tutorialButton:addEventListener ("touch", pressTutorial) exitButton:addEventListener ("touch", pressExit) end function scene:exitScene(event) playButton:removeEventListener ("touch", pressPlay) aboutButton:removeEventListener ("touch", pressAbout) tutorialButton:removeEventListener ("touch", pressTutorial) exitButton:removeEventListener ("touch", pressExit) end function scene:destroyScene(event) local screenGroup = self.view end local function onKeyEvent( event ) print("event happened") local phase = event.phase local keyName = event.keyName if ( "back" == keyName and phase == "down" ) then native.requestExit() end if ( keyName == "volumeUp" and phase == "down" ) then local masterVolume = audio.getVolume() if ( masterVolume \< 1.0 ) then masterVolume = masterVolume + 0.1 audio.setVolume( masterVolume ) end return true elseif ( keyName == "volumeDown" and phase == "down" ) then local masterVolume = audio.getVolume() if ( masterVolume \> 0.0 ) then masterVolume = masterVolume - 0.1 audio.setVolume( masterVolume ) end return true end return false --SEE NOTE BELOW end --add the key callback Runtime:addEventListener( "key", onKeyEvent ) scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene
Personally I would put the key handling in your main.lua before you call storyboard.gotoScene(). The code you have looks right. The way you have it written, your app should exit when the back button is pressed.
Also, you should put your 2 second timer inside the the splash.lua’s enterScene() function.