Question about physics.pause

I have a simple code.

I want to make the all fall again everytime.

I use collision and physics.

I used the logic and the commands I found on Corona Docs.

but it’s not working.

I do need your help.

Thank you for your help in advanced.

this is the code

local physics = require "physics" physics.setDrawMode( "normal" ) -- "debug" -- "hybrid" -- "normal" physics.start() local function makeBall()     ball = display.newCircle(300, 20, 30)     ball.name = "ball"     physics.addBody( ball, "dynamic", { density = 1.0, friction = 0.1, bounce = .9, radius = 17} ) end local function onCollisionRect(self, event)     if event.other.name == "ball" then             physics.start( false )                 display.remove( ball )             makeBall()             physics.start( true )            end       end rect = display.newRect(200, 700, 300, 10) physics.addBody( rect, "static", { density = 1.0, friction = 0.1, bounce = .9} ) rect.collision = onCollisionRect rect:addEventListener("collision", rect) makeBall()

The ball falls fine, the first time

then I remove the ball fine

recreate a new one

and no physics…

thanks again

Why do you call the physics.start in the collision function?

Try removing those.

You should also nil out the ball after removing it.

I took physics out.

and I nil to ball…

and still the ball don’t falls again.

local physics = require "physics" physics.setDrawMode( "normal" ) -- "debug" -- "hybrid" -- "normal" physics.start() local function makeBall()     ball = display.newCircle(300, 20, 30)     ball.name = "ball"     physics.addBody( ball, "dynamic", { density = 1.0, friction = 0.1, bounce = .9, radius = 17} ) end local function onCollisionRect(self, event)     if event.other.name == "ball" then                display.remove( ball )             ball = nil             makeBall()            end       end rect = display.newRect(200, 700, 300, 10) physics.addBody( rect, "static", { density = 1.0, friction = 0.1, bounce = .9} ) rect.collision = onCollisionRect rect:addEventListener("collision", rect) makeBall()

Any idea?

Yes i know. You need to put in a small delay between the collision event and creating a new the ball.

like this:

timer.performWithDelay ( 10, makeBall )

From the corona docs:

You should not attempt to add a physics body during a collision event. Instead, wait until the next application cycle.

I change the code for this

local physics = require "physics" physics.setDrawMode( "normal" ) -- "debug" -- "hybrid" -- "normal" physics.start() local function makeBall()     ball = display.newCircle(300, 20, 30)     ball.name = "ball"     physics.addBody( ball, "dynamic", { density = 1.0, friction = 0.1, bounce = .9, radius = 17} ) end local function onCollisionRect(self, event)     if event.other.name == "ball" then                display.remove( ball )             ball = nil             makeBall()            end       end rect = display.newRect(200, 700, 300, 10) physics.addBody( rect, "static", { density = 1.0, friction = 0.1, bounce = .9} ) rect.collision = onCollisionRect rect:addEventListener("collision", rect) timer.performWithDelay ( 20, makeBall )

And the ball still is there not falling…

you can run this code in your computer

and you can see…

I know, it might be very difficult to do this…

The problem is you are creating a new ball while a collision is going on,

that’s why you need to put in a small delay, so the new ball will be created after the collision is over.

So I meant like this:

local physics = require “physics”
physics.setDrawMode( “normal” ) – “debug” – “hybrid” – “normal”
physics.start()

local function makeBall()
    ball = display.newCircle(300, 20, 30)
    ball.name = “ball”
    physics.addBody( ball, “dynamic”, { density = 1.0, friction = 0.1, bounce = .9, radius = 17} )
end

local function onCollisionRect(self, event)
    if event.other.name == “ball” then   
            display.remove( ball )
            ball = nil
            timer.performWithDelay ( 20, makeBall )
           end  
    end

rect = display.newRect(200, 700, 300, 10)
physics.addBody( rect, “static”, { density = 1.0, friction = 0.1, bounce = .9} )
rect.collision = onCollisionRect
rect:addEventListener(“collision”, rect)

makeBall( )

Thank you very much

you are the best

that works great!!!

thank you…

Why do you call the physics.start in the collision function?

Try removing those.

You should also nil out the ball after removing it.

I took physics out.

and I nil to ball…

and still the ball don’t falls again.

local physics = require "physics" physics.setDrawMode( "normal" ) -- "debug" -- "hybrid" -- "normal" physics.start() local function makeBall()     ball = display.newCircle(300, 20, 30)     ball.name = "ball"     physics.addBody( ball, "dynamic", { density = 1.0, friction = 0.1, bounce = .9, radius = 17} ) end local function onCollisionRect(self, event)     if event.other.name == "ball" then                display.remove( ball )             ball = nil             makeBall()            end       end rect = display.newRect(200, 700, 300, 10) physics.addBody( rect, "static", { density = 1.0, friction = 0.1, bounce = .9} ) rect.collision = onCollisionRect rect:addEventListener("collision", rect) makeBall()

Any idea?

Yes i know. You need to put in a small delay between the collision event and creating a new the ball.

like this:

timer.performWithDelay ( 10, makeBall )

From the corona docs:

You should not attempt to add a physics body during a collision event. Instead, wait until the next application cycle.

I change the code for this

local physics = require "physics" physics.setDrawMode( "normal" ) -- "debug" -- "hybrid" -- "normal" physics.start() local function makeBall()     ball = display.newCircle(300, 20, 30)     ball.name = "ball"     physics.addBody( ball, "dynamic", { density = 1.0, friction = 0.1, bounce = .9, radius = 17} ) end local function onCollisionRect(self, event)     if event.other.name == "ball" then                display.remove( ball )             ball = nil             makeBall()            end       end rect = display.newRect(200, 700, 300, 10) physics.addBody( rect, "static", { density = 1.0, friction = 0.1, bounce = .9} ) rect.collision = onCollisionRect rect:addEventListener("collision", rect) timer.performWithDelay ( 20, makeBall )

And the ball still is there not falling…

you can run this code in your computer

and you can see…

I know, it might be very difficult to do this…

The problem is you are creating a new ball while a collision is going on,

that’s why you need to put in a small delay, so the new ball will be created after the collision is over.

So I meant like this:

local physics = require “physics”
physics.setDrawMode( “normal” ) – “debug” – “hybrid” – “normal”
physics.start()

local function makeBall()
    ball = display.newCircle(300, 20, 30)
    ball.name = “ball”
    physics.addBody( ball, “dynamic”, { density = 1.0, friction = 0.1, bounce = .9, radius = 17} )
end

local function onCollisionRect(self, event)
    if event.other.name == “ball” then   
            display.remove( ball )
            ball = nil
            timer.performWithDelay ( 20, makeBall )
           end  
    end

rect = display.newRect(200, 700, 300, 10)
physics.addBody( rect, “static”, { density = 1.0, friction = 0.1, bounce = .9} )
rect.collision = onCollisionRect
rect:addEventListener(“collision”, rect)

makeBall( )

Thank you very much

you are the best

that works great!!!

thank you…