0
votes

I'm using Corona's physics engine and running into a problem. When a collision occurs, self and event are nil, even though they have a table value (my understanding is that tables can hold any value except nil).

function onCollision(self,event)
    print(event); -- output -> table: 097EF680
    print(self); -- output -> table: 098349D0
    if (event.phase == "began") then
        print(self.myName .. ": collision began with " .. event.other.myName)
    end
end

The if statement should print which two objects are colliding, however I get the error

...\main.lua:270: attempt to concatenate field 'myName' (a nil value)

which is what lead me to add the print statements. The collision IS being detected since this error only happens on a collision, and those two values SHOULD exist since they have an ID. But attempting to access them (through event.myName, event[1]) returns a nil value.

myName is of course defined and not nil for the two objects that are colliding.

1
Can you show your addEventListener call(s) please - Oliver

1 Answers

0
votes

Make sure that you are adding the event handler by using the ':' syntax in lua, so it sends 'self' . Like this:

crate1:addEventListener( "collision", crate1 )

Now, you need to define the 'collision' function for the crate1 object: The 'self' is implied when using ':'

function crate1:collision(event)
   ...
end`

If you need more examples look at the corona docs: http://docs.coronalabs.com/guide/physics/collisionDetection/index.html