Hi
I have a question on overlay. I wrote this code to test out overlay but some how, it calls the overlay code twice. Everything works fine. Parameters are passed between scene and overlay correctly and overlay calling parent scene function correctly as well.
I am stumped why scene1 calls the overlay twice? I attached my screen shots (phone and console).
Any advice appreciated.
Thanks
TH
My main scene code:
–
– scene.lua
–
display.setDefault(“background”,1,1,1)
local composer = require( “composer” )
local cX = display.contentCenterX
local cY = display.contentCenterY
– Load scene with same root filename as this file
local scene = composer.newScene( )
– FUNCTION TO BE CALLED BY OVERLAY OLAY1
function scene:doOlay1(e)
print(“doOlay1”,e)
end
function scene:create( event )
local sceneGroup = self.view
end
function scene:show( event )
local sceneGroup = self.view
local phase = event.phase
if phase == “will” then
– Called when the scene is still off screen and is about to move on screen
elseif phase == “did” then
local Olay = display.newText(“SHOW OVERLAY”, cX,cY,native.systemFont,12)
Olay:setFillColor({0,0,0})
local function doOL(event)
print(“OL clicked”)
local options = {
isModal = true,
params = {sampleVar = “my sample variable”}
}
composer.showOverlay( “olay1”, options )
end
Olay:addEventListener(“tap”,doOL)
end
end
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if event.phase == “will” then
– Called when the scene is on screen and is about to move off screen
–
– INSERT code here to pause the scene
– e.g. stop timers, stop animation, unload sounds, etc.)
elseif phase == “did” then
– Called when the scene is now off screen
end
end
function scene:destroy( event )
local sceneGroup = self.view
– Called prior to the removal of scene’s “view” (sceneGroup)
–
– INSERT code here to cleanup the scene
– e.g. remove display objects, remove touch listeners, save state, etc
end
– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )
return scene
My Overlay Code:
local composer = require( “composer” )
local scene = composer.newScene()
local cX = display.contentCenterX
local cY = display.contentCenterY
local parent
function scene:hide( event )
local sceneGroup = self.view
local phase = event.phase
if ( phase == “will” ) then
end
end
function scene:show(event)
print(“overlay1 show”)
local sceneGroup = self.view
local lGroup = display.newGroup()
local sample = event.params.sampleVar
parent = event.parent
local function doDone(event)
print(“done click”)
composer.hideOverlay(“fade”,1000)
parent:doOlay1(“HELLO”)
end
local function doCancel(event)
print(“cancel click”)
composer.hideOverlay()
parent:doOlay1(nil)
end
local title = display.newText(sample, cX,50,native.systemFont,12)
title:setFillColor(0,0,0)
lGroup:insert(title)
local cancel = display.newRoundedRect(cX-50,170,100,30,10)
cancel:setFillColor(0,0,0)
cancel.txt=display.newText(“CANCEL”,cX-50,170,native.systemFont,20)
cancel.txt:setFillColor(1,1,1)
cancel:addEventListener(“tap”,doCancel)
lGroup:insert(cancel)
lGroup:insert(cancel.txt)
local done = display.newRoundedRect(cX+50,170,100,30,10)
done:setFillColor(0,0,0)
done.txt=display.newText(“DONE”,cX+50,170,native.systemFont,20)
done.txt:setFillColor(1,1,1)
done:addEventListener(“tap”,doDone)
lGroup:insert(done)
lGroup:insert(done.txt)
sceneGroup:insert(lGroup)
end
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
return scene