Problems with collision events

I am building a game where you blow up asteroids and I need a way to detect if you are colliding with an asteroid, The asteroids are in an array but when a collision event happens, the tag says nil, so I can’t tell what I am colliding with. Please help!

This is the function that creates the asteroid (Is ran every 1.5 seconds)

function newAsteroid(x,y)


** local x = x**
** local y = y**
** local width = 30**
** local height = 30**


** local obj = display.newImage(“rock.png”, x, y)**
** obj.width = 100**
** obj.height = 100**


** obj.objectType = “asteroid”**


** physics.addBody(obj, {friction = 0})**


** table.insert(asteroids, obj)**


end

And this is the collision function

function playerCollision(event)

** if(event.target.objectType == “asteroid”) then**


** display.remove(event.target)**
** table.remove(asteroids, 1)**


** end**


end

Thank You in Advance!!

Please use proper code formatting.

The global collision handler’s event includes event.object1 and event.object2 whereas you are looking for event.target. If you change that bit, then it’ll start working.

See documentation: https://docs.coronalabs.com/guide/physics/collisionDetection/index.html#collision-handling

1 Like

Thank you, but I tried the code, and it still does not work ):

What did you try specifically? Could you share your new code?

I actually just got it to work. I used event.other (: