How to avoid collision event after resume function is called?

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

I just test again and that approach doesn’t work. The collision event is still fired if the Pause button is touched when the player is at a certain distance from other object (then 100ms will give enough time to fire the collision and pause get called just before we can cancel the timer). I think even if I increase the delay, it will not solve the problem.

Anyone share me how you do with your game?

Thank you.

So Lin

Hi So Lin,

Do you pause the physics engine at the same time the game pauses?

Brent

Hi Brent,

Pausing the physics engine solve the issue. Thank you.

So Lin

I just test again and that approach doesn’t work. The collision event is still fired if the Pause button is touched when the player is at a certain distance from other object (then 100ms will give enough time to fire the collision and pause get called just before we can cancel the timer). I think even if I increase the delay, it will not solve the problem.

Anyone share me how you do with your game?

Thank you.

So Lin

Hi So Lin,

Do you pause the physics engine at the same time the game pauses?

Brent

Hi Brent,

Pausing the physics engine solve the issue. Thank you.

So Lin