About Collision 'contact'

Dear everybody,

I have some question.

When I use a collision like as follows

local function CollisionFun(event)

       local CollisionObject = event.other 

       if collideObject.surfaceType == Enemy[i].surfaceType then

             if event.contact.isTouching == true then

print(“attack”)

              end

       end

end

Most of the time this operation is not a problem,but sometimes he will appear Runtime error

.

attempt to index field ‘contact’ (a nil value)

stack traceback:

in function ‘?’

In general, after the object is removed, the Collision Listener of the object will be canceled.

Then to completely avoid this problem, should I manually remove it?

Like under print(“attack”), add this line.

event.target:removeEventListener( “collision”, CollisionFun) 

May I ask if I should do this to make sure there is no problem?

Thanks everyone.

Hi,

I haven’t done any collisions yet (believe it or not) but from a logical point of view, you are right on track.

If the event listener fires after the object has been removed, you can get this error.

Now you say you remove the listener just after the object so, which i likely to explain why it only happens occasionally.

Its a very small time window.

Try to remove it first, before you remove the object itself, and hopefully the issue will be no more.

Make a few tests to be sure, if you haven’t already, as again this is all theory for me  :slight_smile:

OK

Thanks you anaqim.

I will use this method to solve this problem.

:smiley:

Hi,

I haven’t done any collisions yet (believe it or not) but from a logical point of view, you are right on track.

If the event listener fires after the object has been removed, you can get this error.

Now you say you remove the listener just after the object so, which i likely to explain why it only happens occasionally.

Its a very small time window.

Try to remove it first, before you remove the object itself, and hopefully the issue will be no more.

Make a few tests to be sure, if you haven’t already, as again this is all theory for me  :slight_smile:

OK

Thanks you anaqim.

I will use this method to solve this problem.

:smiley: