How can I correctly pause a game involving physics and transitions?
I want to include a pause button in my game which involves physics on the objects and some transitions. How can I do that in Corona SDK, to which开发者_如何学JAVA I am fairly new? Any guidance??
function pause()
physics.pause()
end
Runtime:addEventListener("touch", pause)
will pause all physics.
function pause(event)
if event.phase == "began" then
if paused == false then
physics.pause()
paused = true
elseif paused == true then
physics.start()
paused = false
end
end
end
paused = false
Runtime:addEventListener("touch", pause)
will toggle physics pause on click
Besides stopping physics, you also have to remove the enterframe listeners, if you have objects that moves with the scene, and any animation you have in the game.
So your pause function would be like that
function Pause(event)
if event.phase == "began" then
if paused == false then
physics.pause()
paused = true
Runtime:removeEventListener("enterFrame",Contador_func)
Runtime:removeEventListener("enterFrame",Move_c1ScrollPai)
Runtime:removeEventListener("enterFrame",Move_c1ScrollFilho)
Runtime:removeEventListener("enterFrame",VelAumenta)
Runtime:removeEventListener("enterFrame",Serra)
Runtime:removeEventListener("enterFrame",passaro)
Runtime:removeEventListener("enterFrame",bf)
Runtime:removeEventListener("enterFrame",bf2)
Runtime:removeEventListener("enterFrame",p)
Runtime:removeEventListener("enterFrame",CR)
Runtime:removeEventListener("enterFrame",Funcao)
Runtime:removeEventListener("enterFrame",BumPassaro)
Runtime:removeEventListener("collision",Andar)
p:pause()
Bum:pause()
coin:pause()
coins:pause()
elseif paused == true then
Runtime:addEventListener("enterFrame",Contador_func)
Runtime:addEventListener("enterFrame",Move_c1ScrollPai)
Runtime:addEventListener("enterFrame",Move_c1ScrollFilho)
Runtime:addEventListener("enterFrame",VelAumenta)
Runtime:addEventListener("enterFrame",Serra)
Runtime:addEventListener("enterFrame",passaro)
Runtime:addEventListener("enterFrame",bf)
Runtime:addEventListener("enterFrame",bf2)
Runtime:addEventListener("enterFrame",p)
Runtime:addEventListener("enterFrame",CR)
Runtime:addEventListener("enterFrame",Funcao)
Runtime:addEventListener("enterFrame",BumPassaro)
Runtime:addEventListener("collision",Andar)
Runtime:removeEventListener("enterFrame",p)
p:play()
Bum:play()
coin:play()
coins:play()
physics.start()
paused = false
end
end
end
With physics.pause() there will be a slight jitter in the physics object.
Another way is to capture the screen when paused and put it over the scene.
After that you put on top of the screen the paused screen GUI objects.
Remove the paused screen objects and the screen capture when resume.
local gameScene= display.newGroup()
local myObject1 = display.newRect(50,50,100,150 )
gameScene:insert(myObject1)
function onPause(event)
local screenCap = display.captureScreen(false) --dont save to album
gameScene:insert(screenCap)
--insert pause buttons and etc here
end
Runtime:addEventListener("touch",onPause)
Use pause button to set function:
local function pausebtn(event)
if event.phase == "ended" then
physics.pause()
end
pausebtn:addEventListener("touch", pausebtn)
精彩评论