Physics Body do not pause

I am using physics bodies in my game where I have a pause button to pause all my physics bodies.The problem is that just the screen pauses but the physics bodies do not pause,the physics bodies keep moving.
Can anybody help me? [import]uid: 82446 topic_id: 15641 reply_id: 315641[/import]

Can you share the code on your pause button, please?

I assume you are moving your physics bodies using physics and not transitions?

Peach [import]uid: 52491 topic_id: 15641 reply_id: 57737[/import]

Ya.That’s true.here it is how I have gone

function enemy()
local enimg = display.newImage (“enemy.png”)
enimg .x= math.random(10,1000)
enimg .y=-10
enimg .myName = “Drop”
physics.addBody( enimg , { density=0.9, friction=0.3} )
print(“Droping an enemy”)

end
local fall= timer.performWithDelay( 1500, enemy, -1 )

local plaimg = display.newImage (“plane.png”)
plaimg.x=display.viewableContentWidth/2
plaimg.y=display.viewableContentHeight/1.1
plaimg.myName = “Plane”
physics.addBody( plaimg , { density=0.9, friction=0.3} )

local function drag (event)
plaimg .x = event.x
end
plaimg :addEventListener (“touch”, drag)

then i have used a Button where i pause the physics body.
What really happens is the background pauses but this drop keeps on falling not down but just above the display screen and when I resume it all the enemy falls down at once. Which means that they haven’t stopped. More Over my Plane also moves which i don’t want.This is all about my problem. [import]uid: 82446 topic_id: 15641 reply_id: 57763[/import]

Oh I understand!

Pausing physics is working perfectly! What is wrong is that you haven’t paused or cancelled your timer, so enemies/drops keep spawning!

What you want to do is;

If you are using version .602 or above add this to your pause function;
[lua]timer.pause(fall)[/lua]

Then resume it when you unpause.

OR if you are using version .591, the latest stable release, add this to the pause function;
[lua]timer.cancel(fall)[/lua]
And then add this on resume;
[lua]local fall= timer.performWithDelay( 1500, enemy, -1 )[/lua]

This is because pausing physics does not effect timers; timers are not a part of physics :slight_smile:

Peach [import]uid: 52491 topic_id: 15641 reply_id: 57944[/import]