Bug for simple game

Ok so basically me and my friend are creating a game with collisions. we are having a problem where once the collision is done, the game crashes. if someone could look at this code its really not all that much it would mean alot and if you could possibly come up with a solution that would be even better. here are all the collision code we have

function onCollision(event)

–print(“collide!”)

happy:removeSelf()

end

Runtime:addEventListener(“collision”, onCollision)

local function onCollision( self, event )

if ( event.phase == “began” ) then

– Check if body still exists before removing

if( happy) then

happy:removeSelf()

happy = nil

end

end

end

Hello @megabird87,

I see two issues here:

  1. you’re using both a “local” style and “global” style collision listener. You only need one type. It’s your choice which works best for your scenario. If you only have one main object to sense collisions on (like 1 character), then the local type is probably best.

  2. you’ve set the same name to these two functions (“onCollision”). Functions should never share the same name.

Hope this helps,

Brent

Thank you it worked!

Hello @megabird87,

I see two issues here:

  1. you’re using both a “local” style and “global” style collision listener. You only need one type. It’s your choice which works best for your scenario. If you only have one main object to sense collisions on (like 1 character), then the local type is probably best.

  2. you’ve set the same name to these two functions (“onCollision”). Functions should never share the same name.

Hope this helps,

Brent

Thank you it worked!