This is driving me crazy… I have a game scene that is showing a pause menu using an overlay scene. On the overlay scene there are buttons including a resume game and restart level button (created using widget.newButton()
. When showing the overlay, I use isModal = true
which works fine in that the game scene does not receive touch events through the overlay scene.
When you hit the resume game button, the onRelease function fires a runtime event that is picked up by a listener on the game scene below, which hides the overlay and resumes the game… however, at that point the game receives a touch event right below where the resume button was!
It’s like the overlay scene is removed so quickly the touch event is still firing, even though hideOverlay() is not being triggered until the release phase. The same happens with the restart level button - the whole game resets and a touch event is immediately triggered below the button. I’ve tried moving the hideOverlay() function call around in my resume and restart functions and using transition time on hideOverlay() but I can’t fix this… any ideas much appreciated.
This is the pause and resume functions from game-scene.lua:
[lua]function scene:gamePaused(event)
print(“Pausing game”)
– Add event listeners for pause menu buttons
Runtime:addEventListener(“resumeGame”, scene)
Runtime:addEventListener(“restartGame”, scene)
Runtime:addEventListener(“quitGame”, scene)
local options = {
effect = “fade”,
time = 500,
isModal = true
}
storyboard.showOverlay(“scripts.menu.pause”, options)
return true
end
function scene:resumeGame(event)
print(“Resuming game”)
– Hide the pause menu
Runtime:removeEventListener(“resumeGame”, scene)
Runtime:removeEventListener(“restartGame”, scene)
Runtime:removeEventListener(“quitGame”, scene)
storyboard.hideOverlay(“fade”, 500)
– Resume game
game:resume()
return true
end
function scene:restartGame(event)
print(“Restarting level”)
– Reset game
game:quit()
local gameView = game:new(level.params)
self.view:insert(gameView)
game:start()
– Hide the menu
Runtime:removeEventListener(“resumeGame”, scene)
Runtime:removeEventListener(“restartGame”, scene)
Runtime:removeEventListener(“quitGame”, scene)
storyboard.hideOverlay(“fade”, 500)
return true
end[/lua]
And this is from menu/pause.lua:
[lua]-----------------------------------------------------------------------------------------
– Pause Menu (in game)
local storyboard = require(“storyboard”)
local scene = storyboard.newScene()
local widget = require(“widget”)
local resumeBtn
local restartBtn
local quitBtn
– EVENT HANDLERS
local function resumeBtnRelease(event)
if (event.phase == “release”) then
Runtime:dispatchEvent({name = “resumeGame”})
end
return true
end
local function restartBtnRelease(event)
Runtime:dispatchEvent({name = “restartGame”})
return true
end
local function quitBtnRelease(event)
Runtime:dispatchEvent({name = “quitGame”})
return true
end
– SCENE EVENTS
– Called when the scene’s view does not exist:
function scene:createScene(event)
print(“Showing pause menu”)
– background overlay
local background = display.newRect(0, 0, display.contentWidth, display.contentHeight)
background:setFillColor(0, 0, 0) – black background
background.alpha = 0.8
– “Game paused” title
local pausedText = display.newText(“Game paused”,
0,
0,
“Helvetica-Light”,
20
)
pausedText:setReferencePoint(display.CenterReferencePoint)
pausedText.x = display.contentCenterX
pausedText.y = display.contentWidth * 0.1
– Resume button
resumeBtn = widget.newButton{
label = “Resume game”,
labelColor = {
default = {0},
over = {128}
},
font = “Helvetica-Light”,
fontSize = 20,
width = 150,
emboss = true,
onRelease = resumeBtnRelease
}
resumeBtn.x = display.contentCenterX
resumeBtn.y = display.contentHeight * 0.3
– Restart button
restartBtn = widget.newButton{
label = “Restart level”,
labelColor = {
default = {0},
over = {128}
},
font = “Helvetica-Light”,
fontSize = 20,
width = 140,
emboss = true,
onRelease = restartBtnRelease
}
restartBtn.x = display.contentCenterX
restartBtn.y = display.contentHeight * 0.5
– Quit button
quitBtn = widget.newButton{
label = “Quit game”,
labelColor = {
default = {0},
over = {128}
},
font = “Helvetica-Light”,
fontSize = 20,
emboss = true,
onRelease = quitBtnRelease
}
quitBtn.x = display.contentCenterX
quitBtn.y = display.contentHeight * 0.7
self.view:insert(background)
self.view:insert(pausedText)
self.view:insert(resumeBtn)
self.view:insert(restartBtn)
self.view:insert(quitBtn)
end[/lua] [import]uid: 164840 topic_id: 33320 reply_id: 333320[/import]