Get Collided object names returns nil

I’ve registered collision as below

Runtime:addEventListener("collision", onCollision)  

on collision I"m trying to get the object names as below

local function onCollision(event) if ( event.phase == "began" ) then         print(event.contact.isTouching)         print(event.object1.myName)     end end  

But the event.object1.myName or event.object1.myName always returns nil

I’m newbie so couldn’t figure out. Even Google didn’t help. Some help appreciated.

Hey,

  1. Are you saying that “event.contact.isTouching” is also nil? Above you have written “But the event.object1.myName or event.object1.myName always returns nil”

If you’re asking why event.contact.isTouching is nil then that is because event.contact is only detected in preCollisions - see http://docs.coronalabs.com/api/event/preCollision/contact.html

  1. FYI, “myName” isn’t part of the API but just a property that you can set on a particular object.  Have you actually set a “myName” property on the object in your collision?  If not, then that would be what you need to do.

For example, if you’re detecting collision between boxes you will need to provide each box in your collision with a value for “myName”

local box = display.newImage( “box.png”, 10, 10)
box.myName = “my box”

Check out this useful guide to get you started http://developer.coronalabs.com/content/game-edition-collision-detection

RIch

Thanks that makes sense. “event.contact.isTouching”  is working just fine. Problem was with the name. So I had to give name to objects manually. Gr8 will do that way. 

Thanks alot for your.

No problem

Hey,

  1. Are you saying that “event.contact.isTouching” is also nil? Above you have written “But the event.object1.myName or event.object1.myName always returns nil”

If you’re asking why event.contact.isTouching is nil then that is because event.contact is only detected in preCollisions - see http://docs.coronalabs.com/api/event/preCollision/contact.html

  1. FYI, “myName” isn’t part of the API but just a property that you can set on a particular object.  Have you actually set a “myName” property on the object in your collision?  If not, then that would be what you need to do.

For example, if you’re detecting collision between boxes you will need to provide each box in your collision with a value for “myName”

local box = display.newImage( “box.png”, 10, 10)
box.myName = “my box”

Check out this useful guide to get you started http://developer.coronalabs.com/content/game-edition-collision-detection

RIch

Thanks that makes sense. “event.contact.isTouching”  is working just fine. Problem was with the name. So I had to give name to objects manually. Gr8 will do that way. 

Thanks alot for your.

No problem