WARNING: physics.removeBody( ) ... Problem!

Hi everyone…

I’m trying to remove the physics on an object, so it will stay still, “static” not able to move it or touch it

like a normal .png image

I found out the

local actualBug1X, actualBug1Y = bug1:localToContent( 0,0 )

to detect the actual location of an image… it works!

so I have this code

&nbsp;&nbsp;&nbsp; -- if the bug goes out of the screen then put it back in this place so I can see it &nbsp;&nbsp;&nbsp; if actualBug1Y \>= 400 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; bug1.x = display.contentWidth - 15 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;bug1.y = display.contentHeight - 200 &nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; -- but if the bug goes to the left of the screen in this place then remove the physics &nbsp;&nbsp;&nbsp; -- so I won't move it anymore &nbsp;&nbsp; &nbsp;if actualBug1X \<= 80 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;transition.to(bug1, {time=2000, x=20, y=display.contentHeight - 15, rotation = 2}) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;physics.removeBody(bug1, "static", {density = 1.0, friction = 0.9, bounce = 0.5, radius=10, isSensor = true}) &nbsp;&nbsp; &nbsp;end

But when I do that, I get the WARNING in terminal

WARNING: physics.removeBody( ) given a display object than is not a physics object

The app works fine in simulator, but I don’t like to have WARNINGS

Thanks for your help

um you dont need all those other parameters that you passed, maybe do a simple test

if not ( physics.removeBody( bug1 ) ) then print( "Could not remove physics body" ) end

(copied staright from the docs)

How are you adding the physics bodies, maybe you could post that code?

– in the enterScene

if actualBug1X \<= 80 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;transition.to(bug1, {time=2000, x=20, y=display.contentHeight - 15, rotation = 2}) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;physics.removeBody( bug1 ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if not ( physics.removeBody( bug1 ) ) then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print( "Could not remove physics body" ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;end

In the terminal I see the WARNING: and the “Could not remove physics body”

but in the simulator I can not touch the bug, there is no physics in that image(object)

When I create the bug I add the physics

– In the createScene

&nbsp;&nbsp;&nbsp; bug1 = display.newImageRect( "images/lady1.png", 60, 60 ) &nbsp;&nbsp;&nbsp; group:insert(bug1) &nbsp;&nbsp;&nbsp; bug1.x = display.contentWidth - 15 &nbsp;&nbsp;&nbsp; bug1.y = display.contentHeight - 200 &nbsp;&nbsp;&nbsp; bug1:scale(.5, .5) &nbsp;&nbsp;&nbsp; physics.addBody(bug1, "dynamic", {density = 1.0, friction = 0.9, bounce = 0.5, radius=10, isSensor = false})

I don’t get it

Hi @helloworld2013d,

What is this code? I don’t understand why you’re trying to detect a “not” case on the function call after you’ve called the function once.

[lua]

physics.removeBody( bug1 )

if not ( physics.removeBody( bug1 ) ) then

   print( “Could not remove physics body” )

end

[/lua]

It’s probably removing the body and returning “true” (because the body was removed). Then you call the function again, but I don’t know why you need to. Is the body removed? If yes, then why do a print() check on it?

Take care,

Brent

Hi Brent… I put – if not…

because 86lemonade86 told me in a post above…

but what I’m trying to do is removeBody, but I get a warning

like I explaing above…

— simple

physics.addBody(bug1, “dynamic”, {density = 1.0, friction = 0.9)

and then later in the code

physics.removeBody(bug1, “dynamic”, {density = 1.0, friction = 0.9)

how do I do that without getting the WARNING

 

Is that code within an enterFrame event?
If so, are you stopping it after you’ve removed the body? Because that would explain the warning. It wouldn’t happen on the first time the body gets properly removed, but on subsequent times, when the body is already not there anymore.

For example, you could try:
 

if actualBug1X \<= 80 then transition.to(bug1, {time=2000, x=20, y=display.contentHeight - 15, rotation = 2}) if not bug1.bodyWasRemoved then physics.removeBody(bug1); bug1.bodyWasRemoved = true; end end

Thanks RagdogStudios…

that works! Now I don’t get the waring…

but where did you get this    .bodyWasRemoeved

is a variable name that you created?

or is a command? or a property? or a parameter?

or what?

It’s just a parameter for the bug1 object that we created only for the purpose of checking if a body was already removed or not.
As long as it doesn’t conflict with other display object parameters (such as isVisible, alpha and so on) you can append as many parameters as you want to an object and use them to keep track of different things (;

Thanks

um you dont need all those other parameters that you passed, maybe do a simple test

if not ( physics.removeBody( bug1 ) ) then print( "Could not remove physics body" ) end

(copied staright from the docs)

How are you adding the physics bodies, maybe you could post that code?

– in the enterScene

if actualBug1X \<= 80 then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;transition.to(bug1, {time=2000, x=20, y=display.contentHeight - 15, rotation = 2}) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;physics.removeBody( bug1 ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if not ( physics.removeBody( bug1 ) ) then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print( "Could not remove physics body" ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;end

In the terminal I see the WARNING: and the “Could not remove physics body”

but in the simulator I can not touch the bug, there is no physics in that image(object)

When I create the bug I add the physics

– In the createScene

&nbsp;&nbsp;&nbsp; bug1 = display.newImageRect( "images/lady1.png", 60, 60 ) &nbsp;&nbsp;&nbsp; group:insert(bug1) &nbsp;&nbsp;&nbsp; bug1.x = display.contentWidth - 15 &nbsp;&nbsp;&nbsp; bug1.y = display.contentHeight - 200 &nbsp;&nbsp;&nbsp; bug1:scale(.5, .5) &nbsp;&nbsp;&nbsp; physics.addBody(bug1, "dynamic", {density = 1.0, friction = 0.9, bounce = 0.5, radius=10, isSensor = false})

I don’t get it

Hi @helloworld2013d,

What is this code? I don’t understand why you’re trying to detect a “not” case on the function call after you’ve called the function once.

[lua]

physics.removeBody( bug1 )

if not ( physics.removeBody( bug1 ) ) then

   print( “Could not remove physics body” )

end

[/lua]

It’s probably removing the body and returning “true” (because the body was removed). Then you call the function again, but I don’t know why you need to. Is the body removed? If yes, then why do a print() check on it?

Take care,

Brent

Hi Brent… I put – if not…

because 86lemonade86 told me in a post above…

but what I’m trying to do is removeBody, but I get a warning

like I explaing above…

— simple

physics.addBody(bug1, “dynamic”, {density = 1.0, friction = 0.9)

and then later in the code

physics.removeBody(bug1, “dynamic”, {density = 1.0, friction = 0.9)

how do I do that without getting the WARNING

 

Is that code within an enterFrame event?
If so, are you stopping it after you’ve removed the body? Because that would explain the warning. It wouldn’t happen on the first time the body gets properly removed, but on subsequent times, when the body is already not there anymore.

For example, you could try:
 

if actualBug1X \<= 80 then transition.to(bug1, {time=2000, x=20, y=display.contentHeight - 15, rotation = 2}) if not bug1.bodyWasRemoved then physics.removeBody(bug1); bug1.bodyWasRemoved = true; end end

Thanks RagdogStudios…

that works! Now I don’t get the waring…

but where did you get this    .bodyWasRemoeved

is a variable name that you created?

or is a command? or a property? or a parameter?

or what?

It’s just a parameter for the bug1 object that we created only for the purpose of checking if a body was already removed or not.
As long as it doesn’t conflict with other display object parameters (such as isVisible, alpha and so on) you can append as many parameters as you want to an object and use them to keep track of different things (;

Thanks