Simple way to pause game?

Can somebody point me to simple way to pause my game which works with director class?

I have simple physics and sprites in my game “scene”. I want to show pause button on top right corner , which when tapped, pauses the game (physics, sprites and transitions) and shows a popup window saying “Paused” and option to unpause.

I tried coding something simple like below, but it’s not very stable and my physics object don’t resume there original states.

The gameListeners() function is the function in the same file which adds or removes all the event listeners based on function parameter.

[lua]function pause()

gameListeners(‘remove’)
physics.stop()

local pauseScreen = display.newGroup()
localGroup:insert(pauseScreen)
local pauseWindow = display.newRect(pauseScreen, screenWidth*0.5-100, screenHeight*0.5-75, 200, 150 )
local msg = display.newText(pauseScreen, “Game Paused”, pauseWindow.x-30, pauseWindow.y-50, “Helvetica”, 20)
local resume = display.newText(pauseScreen, “Resume”, pauseWindow.x-30, pauseWindow.y+30, “Helvetica”, 20)

pauseWindow:setFillColor(0,0,0)

local function removePauseScreen()
pauseWindow:removeSelf()
msg:removeSelf()
resume:removeSelf()

pauseScreen:removeSelf()
end

local function resumeGame()
removePauseScreen()
physics.start()
gameListeners(‘add’)
end
resume:addEventListener(“tap”, resumeGame)

end[/lua] [import]uid: 48521 topic_id: 9400 reply_id: 309400[/import]

i’ve made a very simple way to make a pause/restart/menu for my physics based game, the secret is to use physics.pause(), and have a goup of buttons with a semi-transparent background that appears when you pause the game:

[lua]-------PAUSE
local menupause = display.newGroup()

local pause = display.newImageRect( “btpause.png”, 97, 40)
pause.x = 384
pause.y = 40
localGroup:insert(pause)

local backgroundpause = display.newRect( 0, 0, 768, 1024 )
backgroundpause:setFillColor(0, 0, 0, 125)
menupause:insert(backgroundpause)

local btresume = display.newImageRect( “btresume.png”, 194, 80)
btresume.x = 384
btresume.y = 400
menupause:insert(btresume)

local btrestart = display.newImageRect( “btrestart.png”, 194, 80)
btrestart.x = 384
btrestart.y = 550
menupause:insert(btrestart)

local btmenu = display.newImageRect( “btmenu.png”, 194, 80)
btmenu.x = 384
btmenu.y = 700
menupause:insert(btmenu)

localGroup:insert(menupause)
menupause.isVisible = false
local function pausegame()
physics.pause()
menupause.isVisible = true
end
pause:addEventListener(“touch”, pausegame)

local function resumegame()
physics.start()
menupause.isVisible = false
end
btresume:addEventListener(“touch”, resumegame)

local function restartgame()
physics.start()
ball_f.x = 655
ball_f.y = 700
shooting = 1
transition.to( block2, { alpha=0, time=300 } )
block2.y = -100
points = 0
balls = 9
scoreText:setText (points … “|POINTS”)
ballsText:setText (balls … “|BALLS”)
menupause.isVisible = false
end
btrestart:addEventListener(“touch”, restartgame)

local function menugame()
TextCandy.CleanUp()
Runtime:removeEventListener( “enterFrame”, startRotation )
director:changeScene(“menu”, “fade”)
end
btmenu:addEventListener(“touch”, menugame)[/lua] [import]uid: 44010 topic_id: 9400 reply_id: 34380[/import]

you can set a flag.

like this:

[lua]-- the flag to run the game or pause the game

local gameIsActive = true
local gameLoop = function ()
if gameIsActive then
– do something
end
end

Runtime:addEventListener(“enterFrame”, gameLoop)

– to pause the phyics and sprite
– on your pause button touch event

local onPause = function (event)

if event.phase == “began” then
– turn the flag to false to puase the Runtime
gameIsActive = false
physics:pause()
– pause all sprites
– pause all audios
end
end

– on your Resume button touch event
– re-activate your physics, audios, sprite, and set gameIsActive = true

– hope this give you an idea[/lua]

Check out my game:

Touch Cannon **FREE**
please rate…:slight_smile: [import]uid: 12455 topic_id: 9400 reply_id: 34460[/import]

Thanks for suggestions guys.

physics:stop() was main culprit in my code… Didn’t know I should be using physics:pause() :slight_smile: [import]uid: 48521 topic_id: 9400 reply_id: 34522[/import]

Good GRAVYYYYYYYYYYYY.

I’ve been trying to pause spawned objects with a pause button and was trying everything under the sun.
I was using an old example with physics.pause() when I should have been using physics:pause().

Almost pushed me over the edge of sanity.

I was trying new timers with the bebe class, new transitions with the transition manager class and nothing would work.
Dang semi colons::::::::::

So, if anyone else was trying to stop of pause spawned objects use physics:pause()

Oh well, thought I’d post.

Thanks

[import]uid: 21615 topic_id: 9400 reply_id: 34636[/import]

Does anybody know how to pause a transition.to?

When my player hits a sensor it moves a object that I used transition.to… When I hit pause it that object keeps moving… I tried pause.physics but didn’t work… [import]uid: 51459 topic_id: 9400 reply_id: 40128[/import]

I had objects that used transition.to and I found this:
http://developer.anscamobile.com/code/pausable-transitions

You may also want to check out the karnak games class and the bebe games class. Karnak has a transition manager and bebe has nice timer classes.

http://karnakgames.com/wp/2011/03/transition-manager-for-corona-sdk-pause-resume-and-manage-all-transitions-in-a-scene/

http://jonbeebe.tumblr.com/post/2320935925/beebegames-class-for-corona-sdk

[import]uid: 21615 topic_id: 9400 reply_id: 40137[/import]