I have multiple menus for my game that I need to be able to call and return from with the parent menu being usable. At this point I can call a new scene (menu) from the main scene (main menu), the called scene works fine. However when I return to the prior menu, the main or even a lower menu that is the parent of the last called scene the buttons no longer respond to events. I can click on them in the simulator and there is no response.
I have read the composer API documentation and looked on the forum. I have tried several different ways to try and get this resolved but not success so far. I am attaching the composer code for bit the main and one of the called scenes. If someone can please point out the mistakes I would appreciate it.
FIRST SCENE:
[lua]
–
– menu.lua
local composer = require(‘composer’) – Scene management
local scene = composer.newScene()
local controller = require(‘libs.controller’) – Gamepad support
local relayout = require(‘libs.relayout’) – Repositions elements on screen on window resize
– include Corona’s “widget” library
local widget = require (‘widget’) – Buttons
– MAIN MENU
function scene:create()
local _W, _H, _CX, _CY = relayout._W, relayout._H, relayout._CX, relayout._CY
--local calledScene = nil
local group = self.view
– MENU IMAGE, ‘buttons’ ARE TRANSPERANT OVER EXISTING ACTION LABELS IN JPG
local backGroundImage1 = ‘images/backgrounds/mainMenuScreen.jpg’
local backGround = display.newImageRect(backGroundImage1,1920, 1200)
backGround.x = display.contentCenterX
backGround.y = display.contentCenterY
relayout.add(backGround)
function onCharacterTouch(event)
if “began” == event.phase then
composer.gotoScene( ‘scenes.character’, “fade”, 1500)
return true
end
end
--SET BUTTON THAT WILL CALL NEXT MENU SCENE
characterButton = widget.newButton({
width = 300,
height = 50,
x = display.contentCenterX,
y = 600,
onEvent = onCharacterTouch
})
group:insert(characterButton)
relayout.add(characterButton)
end
function scene:show(event)
local group = self.view
print(“in show”, parent)
if “did” == event.phase then
– IS THIS MISPLACED?
composer.removeScene(“menu”)
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == “did” ) then
???
end
end
– Called when scene is about to move offscreen:
function scene:exit( event )
local group = self.view
– INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)
end
– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroy( event )
local group = self.view
local shouldRecycle = true
– SHOULD THIS BE HERE AND IS IT REALLY PROVIDING ANYTHING?
– if event.phase == “did” then
– composer.removeScene(“menu”,shouldRecycle)
– end
end
– END OF YOUR IMPLEMENTATION
– “create” event is dispatched if scene’s view does not exist
scene:addEventListener( “create” )
– “enter” event is dispatched whenever scene transition has finished
– is this needed once I return from the called scene ‘character_forum’??
scene:addEventListener( “enter” )
scene:addEventListener( “show” )
scene:addEventListener( “hide” )
– “exit” event is dispatched whenever before next scene’s transition begins
scene:addEventListener( “exit” )
– “destroy” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
– storyboard.purgeScene() or storyboard.removeScene().
scene:addEventListener( “destroy” )
return scene
[/lua]
SECOND SCENE:
[lua]
– CALLED SCENE
local composer = require(‘composer’) – Scene management
local scene = composer.newScene()
local widget = require (‘widget’) – Buttons
local relayout = require(‘libs.relayout’) – Repositions elements on screen on window resize
function scene:create()
local _W, _H, _CX, _CY = relayout._W, relayout._H, relayout._CX, relayout._CY
local group = self.view
– CALLED MENU SCENE FROM MAIN_FORUM SCENE
local characterImage = ‘images/backgrounds/characterProfileScreen.jpg’
local characterBackground = display.newImageRect(characterImage,1920, 1060)
characterBackground.x = display.contentCenterX
characterBackground.y = display.contentCenterY
relayout.add(characterBackground)
– EVENT HANDLER FOR BACK BUTTON, THIS DOES GO BACK TO MAIN MENU
– ISSUE ONCE BACK AT MAIN MENU, MAIN MENU DOES NOT RESPOND TO TOUCH EVENTS
-----------------------------------------------------------------------------------
local function onBackTouch( event )
if “began” == event.phase then
composer.gotoScene(“menu”, “fade”, 1500 )
return true
end
end
– create a widget button to return to main menu
backButton = widget.newButton({
width = 300,
height = 100,
x = 1900 ,
y = 55,
onEvent = onBackTouch
})
group:insert(backButton)
relayout.add(backButton)
end
function scene:show(event)
local group = self.view
local parent = event.parent --reference to the parent scene object, GET A NIL VALUE HERE
if “did” == event.phase then
composer.removeScene(“menu”)
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
local parent = event.parent --reference to the parent scene object, AGAIN A NIL VALUE
if ( phase == “did” ) then
???
-----------------------------------------------------------------------------------
– Call the THE PARENT SCENE, IS THIS NECESSARY TO GET MAIN MENU TO
– RESPOND TO TOUCH EVENTS ONCE WE HAVE RETURNED???
--parent:create()
-----------------------------------------------------------------------------------
end
end
– Called when scene is about to move offscreen:
function scene:exit( event )
local group = self.view
– INSERT code here (e.g. stop timers, remove listenets, unload sounds, etc.)
if event.phase == ‘will’ then
end
end
– If scene’s view is removed, scene:destroyScene() will be called just prior to:
function scene:destroy( event )
local group = self.view
if backButton then
backButton:removeSelf() – widgets must be manually removed
end
end
– “create” event is dispatched if scene’s view does not exist
scene:addEventListener( “create” )
scene:addEventListener( “show”, scene)
scene:addEventListener( “hide”, scene )
– “exit” event is dispatched whenever before next scene’s transition begins
scene:addEventListener( “exit” )
– “destroy” event is dispatched before view is unloaded, which can be
– automatically unloaded in low memory situations, or explicitly via a call to
scene:addEventListener( “destroy” )
return scene
[/lua]