Hi,
I have a problem with collision that sometimes get fired after the pause function is called (the user touch pause button just before the player is about to collide with other object). So what I do to avoid such a problem is to cancel the timer that call the function to pause the game but I have to give enough delay (100ms in this case) for it to work (actually I’m not sure if it really works as I haven’t done much test). The problem to this approach is that when the user press the Pause button under normal circumstances I feel there is a slight delay before the pause screen/scene popup.
I think some of you may face similar issue as too. Could you please share some ideas or/and code to implement pause function in a game that has collision event?
I put my code in different lua file as below:
--pause.lua local function onPause(event ) if event.phase == "ended" then if G.gamePause == false and G.gameStop == false and G.gameRunning == true then G.gamePause = true local function callPause() Runtime:dispatchEvent({name="gamePause"}) end G.pauseTimer = timer.performWithDelay(100,callPause) elseif G.gamePause == true and G.gameStop == false then G.gamePause = false Runtime:dispatchEvent({name="gameResume"}) end end return true end pause:addEventListener("touch", onPause)
--gameplay.lua local function onCollision(event ) local phase = event.phase if( phase == "began" ) then G.gameStop = true if G.gamePause == true then timer.cancel(G.pauseTimer) end -- some codes to stop game end end player:addEventListener("collision", onCollision)
So Lin