Actually this is a really cool thing that Composer does with Overlays.
Back in the Storyboard days, when you closed an overlay, it would trigger the parent’s hideScene function as well as the overlay scene’s hideScene function (or whatever they were named). Composer only calls scene:hide() for the overlay. The parent scene doesn’t get any notification that the overlay is closing. So we added a feature.
In both scene:show() and scene:hide() and possibly scene:create(), the event table includes a member called parent which just so happens to be the actual “scene” object from the parent.
This means you can do things in the parent like:
local scene = composer.newScene() scene.currentLevel = 10 function scene:pause() -- code to pause the scene end function scene:resume() -- code to resume the scene end
Then inside the overlay’s scene:show() during the will phase:
function scene:show( event ) local parent = event.parent local phase = event.phase if "will" == phase then parent:pause() else -- did phase print( parent.currentLevel ) end end function scene:hide( event ) local parent = event.parent local phase = event.phase if "did" == phase then parent:resume() end end
I’m using a similar technique in the game I’m making. I have modules/classes for my enemy ships. They have their own code for managing taking damage and handling their own death and cleanup. It they are killed they use Composer to get the scene object of the game and call a function I’ve attached to the scene object to handle updating the score.
Now you can’t access “local” variables and functions this way. You would have to add it to the scene object, then the overlay would have access to it.
Rob