Correct way to remove physics object due to collision?

mkelly,

One thing I noticed you need to do is wrap the “physics.removeBody(body)” inside an in-line function call. The reason; most callbacks will pass only an event object back to the function. The event object, in your case, is the timer object and not ‘body’ so you may get undesirable results. I always try to be safe and use in-line function calls which also gives the greatest flexibility. The semicolon is optional. (I’ve been a C programer forever.)

timer.performWithDelay(1000,function() physics.removeBody(body); end)  

Jeff

[import]uid: 14119 topic_id: 18290 reply_id: 86853[/import]

hi Danny: can you put that in a statement? I tried:

timer.performWithDelay(1\>2,function() physics.removeBody(body); end)   

I got this:
Runtime error
?:0: attempt to perform arithmetic on a boolean value
stack traceback:
[C]: ?
?: in function ‘performWithDelay’
/Users/mikelly321/Desktop/Space Nuts/level1.lua:133: in function ‘_listener’
?: in function <?:514>
?: in function <?:215>
I subbed 250 in the meantime. below that caused an error with rigidBody removal for some reason.

hi Jeff: I wasn’t getting any problems, but I changed it (even included the :wink: THANKS!!!
[import]uid: 124116 topic_id: 18290 reply_id: 86912[/import]

Sorry no you can’t. I meant I would use 1 or 2 as the number :slight_smile:

timer.performWithDelay(1, whatever) [import]uid: 84637 topic_id: 18290 reply_id: 86913[/import]

I believe what Danny meant was use the number 1 or 2 instead of 1000. The delay can be very short and will work just fine.

timer.performWithDelay(1,function() physics.removeBody(body); end)  

Jeff
[import]uid: 14119 topic_id: 18290 reply_id: 86915[/import]

so 1 = 1/1000 of a second… cool; I got playback errors with 100 though so I think I’m going to bloat on the side of caution perhaps…

thanks, both, again! [import]uid: 124116 topic_id: 18290 reply_id: 86918[/import]

the more I thought about it, the sillier it seemed that the removeBody errors should occur when there was more time given rather than less (seeing how it appeared to be firing more than once). I finally gave in and used 2 and then finally 1, and I can’t repeat the error no matter how hard my character slam dunks his nuts…

an even bigger THANKS, Danny and Jeff. [import]uid: 124116 topic_id: 18290 reply_id: 87215[/import]

Hi

I’m having a problem with the timer.performWithDelay function. I’m not trying to remove an object, just setting the object to invisible and isBodyActive = false. I’m getting

ERROR: display object property isBodyActive cannot be set when the world is locked and in the middle of number crunching, such as during a collision event

i’ve even put a delay of 20 seconds (20000 ms) but still the object disappears immediately but the physic body is still responding to collision events. Code is:

local function removeCueBallOnCollision(self, e)
if (e.phase ~= “ended”) then return true; end;
if (e.other.id == “targetBall”) then
timer.performWithDelay(20000, removeCueBall( self, e) )
end
return true
end

Any help would be appreciated.

Sean
[import]uid: 101690 topic_id: 18290 reply_id: 89209[/import]

One thing is you don’t pass ‘self’ with the collision event, only one parameter is passed; ‘event’.

 local function removeCueBallOnCollision(event)  
 print("Cue Ball Collision")  
 end  
  
 cueBall:addEventListener("collision", removeCueBallOnCollision);  

Also “timer.performWithDelay” passes only the timer object unless you use an in-line function call.

So your code should look something like this:

local function removeCueBallOnCollision(e)  
 if (e.phase ~= "ended") then return true; end;  
 if (e.other.id == "targetBall") then  
 timer.performWithDelay(20000, function() removeCueBall( e.other); end; )  
 end  
return true  
end  

I may be missing something but let me know if this helps.

Jeff

[import]uid: 14119 topic_id: 18290 reply_id: 89215[/import]

Jeff,

I think you must be right that the problem is with the parameters to performWithDelay function, but the addListener code is valid, I believe. The odd thing is that the function referenced by performWithDelay is being called, because the object is no longer visible. The problem is that the delay is not happening.

This way of using the listener event for different instances of the same object is from http://blog.anscamobile.com/2011/06/the-corona-event-model-explained/

– METHOD 2:

local function touchListener( self, event )

– In this example, “self” is the same as event.target

end

– Notice the difference in assigning the event:
myImage.touch = touchListener
myImage:addEventListener( “touch”, myImage )

Thanks for the prompt reply

All the best

Sean [import]uid: 101690 topic_id: 18290 reply_id: 89267[/import]

ok. After a bit more research I understand what’s happening here. When i used this:

timer.performWithDelay(20000, removeCueBall( self, e) )

lua evaluates the the function call “removeCueBall( self, e)” to determine what is the value of the parameter it should pass. This is when that function gets called - and the object is made invisible and Corona throws an error.

The easiest solution is to store the self.id in a variable and call the function correctly :

timer.performWithDelay(20000, removeCueBall )

the function removeCueBall can then look up the variable and do its stuff.

All the best

Sean [import]uid: 101690 topic_id: 18290 reply_id: 89273[/import]

Your right about the addEventListener code. You can use it with the extra parameter but as the blog described, it takes some understanding.
As far as the ‘timer.performWithDelay’ function, the ‘listener’ takes no parameters as I described in my last post, but you can use an in-line function call and pass as many parameters as you like, but in your case, pass a reference to the collision object.

timer.performWithDelay(20000, function() removeCueBall(e.other); end; )  

The use of in-line functions has saved me several times.

Jeff

[import]uid: 14119 topic_id: 18290 reply_id: 89274[/import]

Tried your method, saves me a local variable. Worked fine but only after I removed the semi-colons!
Was getting error “)” expected near “;”
Very odd, but works now. Thanks for the help.

All the best

Sean [import]uid: 101690 topic_id: 18290 reply_id: 89295[/import]

Sorry, I did have an extra semicolon!
I come from a long background of C programming where a semicolon is used to terminate almost every statement.

Glad you got it working.

Jeff
[import]uid: 14119 topic_id: 18290 reply_id: 89302[/import]

thank you very much…

i will face the problem in physics concept it solved with help of your solution [import]uid: 91542 topic_id: 18290 reply_id: 112790[/import]