Simpe "pause overlay menu" problem

I’m trying to add the very simple pause menu as overlay to my “gamemode.lua”. I made this from guide about composer and showOverlay/hideOverlay features, but missing something in the scene logic.

I can see that “pause.lua” loaded and show text’s line, but they are not interact with “tap” event. 

In “gamemode.lua” I have:

local function GamePause () --this before "function scene:create( event )" local options = {     isModal = true,     effect = "fade",     time = 400     }     physics.pause()     timer.pause ()     composer.showOverlay( "pause", options ) end local function GameResume ()     physics.resume ()     timer.resume () end --  PauseButton = display.newRect( different options ) --this inside "function scene:create( event )" PauseButton:addEventListener ("tap", tapPauseButton)

in “pause.lua” I have this:

local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------- print("U are in the pause overlay") local pauseGroup local function gotoMenu()     composer.gotoScene( "menu" ) -- This command should work, but it doesnt too end local function gotoGame ()     composer.hideOverlay( "fade", 400 ) end -- ----------------------------------------------------------------------------------- -- ----------------------------------------------------------------------------------- function scene:create( event )      -- create()     local sceneGroup = self.view     print("U are in the scene:create( event ) part")     pauseGroup = display.newGroup()  -- Display group for the background image     sceneGroup:insert( pauseGroup )     local MenuBackground = display.newImageRect( pauseGroup, "MenuBackground2.png", display.actualContentWidth, display.actualContentHeight )     MenuBackground.x = display.contentCenterX     MenuBackground.y = display.contentCenterY     local buttonBackToMenu = display.newText( sceneGroup, "Back To Menu", display.contentCenterX, 0.3\*display.actualContentHeight, native.systemFont, 100 )     buttonBackToMenu:setFillColor( 0, 0.294, 0.518 )     buttonBackToMenu:addEventListener( "tap", gotoMenu )     local buttonContinue = display.newText( sceneGroup, "Continue", display.contentCenterX, 0.35\*display.actualContentHeight+50, native.systemFont, 100 )     buttonContinue:setFillColor( 0, 0.294, 0.518 )     buttonContinue:addEventListener( "tap", gotoGame ) end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen end end

-- hide() function scene:hide( event ) local sceneGroup = self.view     local phase = event.phase     local parent = event.parent if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen)         parent:GameResume() elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene

I think you over complicating things especially ii you are having problems. Start simple and then add layers. All your files have problems but I’ll start with the first one.

local function GamePause ( event ) --move this to the top of the file not in create. outside everything. local options = { isModal = true, effect = "fade", time = 400 } physics.pause() ---timer.pause () --not sure what this timer is so commented it out for now. composer.showOverlay( "pause", options ) end local function GameResume () --move this to the top of the file not in create. outside everything. physics.resume () --timer.resume () end -- -- PauseButton should probably be a local variable. I don't have all your code but what I have it looks global. -- at the very least put it at the top of the file like: -- local PauseButton PauseButton = display.newRect( different options ) --this inside "function scene:create( event )" PauseButton:addEventListener ("tap", GamePause) -- changed the function to the only function I see.

Today I spent the whole day investigating what I was wrong about. Found it and discover two possibilities how to make it. And I discovered widgets (I’ll create a theme about them later).

Agramonte , thank you for the answer! «not sure what this timer is so commented it out for now» - I’m using timer2.lua so it’s just pause everything, I will cut it now.

So, the first possibility is:

local PauseButton  –on the top of the file

local function GamePause () –still before “function scene:create( event )”

    local options = {

    isModal = true,

    effect = “fade”,

    time = 400

    }

    

    --Pause physics, timers, make objects .isBodyActive=false, etc…

       

        print(“U are in the function GamePause ()”)

    composer.showOverlay( “pause”, options )

end

–Inside “function scene:create( event )”

PauseButton = display.newRect( different options)

PauseButton:addEventListener (“tap”, tapPauseButton)

–After “function scene:create( event )” and before function scene:show( event )

function scene:GameResume () _–_As you can see, I change syntax from function GameResume () to function scene:GameResume ()

–Simply start physics, resume timers, make objects .isBodyActive=true, etc…

end

The main changes are in the pause.lua, where I make buttons via widgets:

local composer = require(‘composer’)

local scene = composer.newScene()

local widget = require(‘widget’)

– -----------------------------------------------------------------------------------

print(“U are in the pause.lua!”)

local function gotoMenu()

    print (“gotoMenu func ACTIVE”)

    composer.gotoScene( “menu” )

end

local function gotoGame ()

    print (“gotoGame func ACTIVE”)

    composer.hideOverlay(“fade”, 400)       

end

– -----------------------------------------------------------------------------------

function scene:create(event)

    local phase = event.phase

    local parent = event.parent

    print(“U are in the scene:create( event ) part”)

    local sceneGroup = self.view

    local MenuBackground = display.newImageRect( sceneGroup, “MenuBackground2.png”, display.actualContentWidth, display.actualContentHeight )

    MenuBackground.x = display.contentCenterX

    MenuBackground.y = display.contentCenterY

    local buttonBackToMenu = widget.newButton({

        x =  display.contentCenterX,

        y = 0.3*display.actualContentHeight,

        label = ‘Back To Menu’,

        fontSize = 100,

        shape = ‘roundedRect’,

        width = 700,

        height = 120,

        fillColor = { default = {1, 1, 1}, over = {0, 0, 1} },

        onRelease = gotoMenu

    })

    self.view:insert(buttonBackToMenu)

    local buttonContinue = widget.newButton({

        x =  display.contentCenterX,

        y = 0.4*display.actualContentHeight,

        label = ‘Continue’,

        fontSize = 100,

        shape = ‘roundedRect’,

        width = 700,

        height = 120,

        fillColor = { default = {1, 1, 1}, over = {0, 0, 1} },

        onRelease = gotoGame

    })

    self.view:insert(buttonContinue)

end

– hide()

function scene:hide( event )

    

    local sceneGroup = self.view

    local phase = event.phase

    local parent = event.parent

    if ( phase == “will” ) then

        – Code here runs when the scene is on screen (but is about to go off screen)

    elseif ( phase == “did” ) then

        – Code here runs immediately after the scene goes entirely off screen

        parent:GameResume()    That command find the scene:GameResume () in the “gamemode.lua” and successfully hide overlay

    end

end

scene:addEventListener( “create”, scene)

scene:addEventListener( “hide”, scene )

return scene

After that, I practice about isModal = true, and It really “prevents touches from passing through the overlay scene to objects in the underlying scene” - but not at all.

I found that if I click/tap on a point outside the buttons, but inside the invisible “game zone”, then the coordinates of the click/tap are determined and the managed object will fly to this point when the overlay will hide.

So this little observation and desire to make code with text+addEventListener motivated me on further experiments. According to my feelings, something just blocks new listeners.

And this is the second possibility:

local PauseButton  –on the top of the file

local function GamePause () –still before “function scene:create( event )”

    Runtime:removeEventListener( “collision”, onCollision )

    Runtime:removeEventListener( “key”, PreventBackAction )

    GameZone:removeEventListener( “tap”, tapMainObject )   –Only this command blocks new listeners

    PauseButton:removeEventListener (“tap”, GamePause )

    local options = {

    isModal = true,

    effect = “fade”,

    time = 400

    }

    

    --Pause physics, timers, make objects .isBodyActive=false, etc…

       

        print(“U are in the function GamePause ()”)

    composer.showOverlay( “pause”, options )

end

–Inside “function scene:create( event )”

PauseButton = display.newRect( different options)

PauseButton:addEventListener (“tap”, tapPauseButton)

–After “function scene:create( event )” and before function scene:show( event )

function scene:GameResume () _–_As you can see, I change syntax from function GameResume () to function scene:GameResume ()

    Runtime:addEventListener( “collision”, onCollision )

    Runtime:addEventListener( “key”, PreventBackAction )

    GameZone:addEventListener( “tap”, tapMainObject )

    PauseButton:addEventListener (“tap”, GamePause )

–Simply start physics, resume timers, make objects .isBodyActive=true, etc…

end

So the pause.lua looks like this:

local composer = require(‘composer’)

local scene = composer.newScene()

– -----------------------------------------------------------------------------------

print(“U are in the pause.lua!”)

local buttonBackToMenu

local buttonContinue

local function gotoMenu()

    print (“gotoMenu func ACTIVE”)

    composer.gotoScene( “menu” )

end

local function gotoGame ()

    print (“gotoGame func ACTIVE”)

    composer.hideOverlay(“fade”, 400)       

end

– -----------------------------------------------------------------------------------

function scene:create(event)

    local phase = event.phase

    local parent = event.parent

    print(“U are in the scene:create( event ) part”)

    local sceneGroup = self.view

    local MenuBackground = display.newImageRect( sceneGroup, “MenuBackground2.png”, display.actualContentWidth, display.actualContentHeight )

    MenuBackground.x = display.contentCenterX

    MenuBackground.y = display.contentCenterY

    buttonBackToMenu = display.newText( sceneGroup, “Back To Menu”, display.contentCenterX, 0.3*display.actualContentHeight, native.systemFont, 100 )

    buttonBackToMenu:setFillColor( 0, 0.294, 0.518 )

    buttonBackToMenu:addEventListener( “tap”, gotoMenu )    --This new listeners were blocked earlier

    buttonContinue = display.newText( sceneGroup, “Continue”, display.contentCenterX, 0.35*display.actualContentHeight+50, native.systemFont, 100 )

    buttonContinue:setFillColor( 0, 0.294, 0.518 )

    buttonContinue:addEventListener( “tap”, gotoGame )    --This new listeners were blocked earlier

end

– hide()

function scene:hide( event )

    

    local sceneGroup = self.view

    local phase = event.phase

    local parent = event.parent

    if ( phase == “will” ) then

        – Code here runs when the scene is on screen (but is about to go off screen)

        buttonBackToMenu:removeEventListener( “tap”, gotoMenu )

        buttonContinue:removeEventListener( “tap”, gotoGame )

    elseif ( phase == “did” ) then

        – Code here runs immediately after the scene goes entirely off screen

        parent:GameResume()

    end

end

scene:addEventListener( “create”, scene)

scene:addEventListener( “hide”, scene )

return scene

Right now I think about which way is better, but the question is – am I wrong with the tap detection? Code is here:

local GameZone = display.newRect( display.contentCenterX, display.contentCenterY, MyWidth, MyHeight)

GameZone:setFillColor( 0, 0.01 )  –Make invisible area for detecting taps

local function tapMainObject  ( event )

    MainObject:setLinearVelocity( 0, 0) –Stop Main Object at the current position

    MainObject: applyForce ( Some Formulas for new  ) –Send Main Object to the new area

    return true

end

–And later on – listener which blocks new listeners in pause.lua:

GameZone:addEventListener( “tap”, tapMainObject )

Sorry you lost me. Maybe somebody with more experience will come along and help.