removeSelf() on collision?

have been trying for many, many hours now without getting it to work and I just cannot find any easy example illustrating just this functionality:

a display object which is a physics body is moving and on hitting a particular wall (display object which is static physics body) should destroy itself.

I have been trying everything and I do get collision events for that moving object but destroying it on impact on particular wall fails and fails and I do not know why.

PLEASE help.

actually, when changing

Runtime:addEventListener
to
object:addEventListener

it works, but I do not know why. Would be happy if someone would explain.

Why it doesn't work depends on your code so unfortunately no easy answer here without seeing code.

Glad you got it working though; if you provide code will try to offer some insight :)

Peach

Are you delaying the removeSelf? You can't remove a physical body during a collision, so I believe you just need to do something along these lines, inside the collision listener:

1
timer.performWithDelay(0, function()  event.target:removeSelf(); event.target = nil; end)

You may or may not need to use a delay; depends on situation. If it works when listener is on obj and not Runtime then that's not a delay issue but rather an event or phase issue, most likely.

But yes, there are many, many times delays are important when playing with physical bodies :)

well, here is the code I use:

--object 1 (o1)
physics.addBody( o1, "dynamic", physicsData:get("o1shape") )
o1.hit = "o1Hit"
o1:addEventListener( "collision", o1 )

--analog for o2, then:

function o1:collision ( event )
if event.other.hit == "o2" then
o1:removeSelf()
end

that works now. what did not work was:
--snip
o1.hit = o1Hit
--snip
function o1Hit ( event )

or

Runtime:addEventListener( "collision", o1 )

when I had the removal of o1 in:

function o2:collision ( event )
if event.other.hit == "o1" then
o1:removeSelf()
end

removal did not work when I was spawning more than 1 o1, i.e. had o1 generation embedded inside a function.

also, now I want to do:

function o1:collision ( event )
if event.other.hit == "o2" then
spriteInstanceO1.x = o3.x
spriteInstanceO1.y = o3.y
spriteInstanceO1(play)
o1:removeSelf()
end

I get an error saying that I was trying to index a global variable spriteInstanceO1 and o3 which is nil.
All those objects are declared as local.

It would really help me with insight into what is going wrong, why sometimes function do not see the objects and sometimes (especially in all the real code others write:) ) they do.

"removal did not work when I was spawning more than 1 o1"

That, I gather, is because you weren't spawning inside a table and allocating unique names.

RE your nil error, where is your o3 object in the code? Above or below the function?

Peach :)

Thank you for the help!!

I have to ponder at some time that table thing and how it is done...

I now put this function lowermost and everything that I have so far in the file does work.

So it matters where the function within a lua file is placed relative to declaring variables? I did not suspect that, I was thinking that everything gets evaluated more or less simultaneously...
So is it ok then to declare all the local variables at the top of the respective file and then placing all the functions? That would maybe be somewhat un-intuitive but safer?

You can forward reference, yes - declaring variables at the top then moving forward.

For the table stuff, it's pretty simple - multipuck has a good example if I recall. (In the CoronaSDK > SampleCode folder, possibly under "Physics".)

Peach :)

Thanks.
This example is good since it is not too complex, good for beginners.

views:2379 update:2012/2/13 9:11:28
corona forums © 2003-2011