Collision bugs

 so a friend and i are trying to make a game where if the zombie collides with the “happy” face, the happy face dissapears. we are having a problem where when the zombie collides with the happy, the star disappears as well. also, when the happy touches the star, both disappear. we want it so that only the star disappears when happy collides with it and when the zombie collides with happy only happy disappears, here is all the collision code –

function onCollision(event)

–print(“collide!”)

happy:removeSelf()

end

Runtime:addEventListener(“collision”, onCollision)

local function onCollision2( self, event )

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

– Check if body still exists before removing

if( happy) then

happy:removeSelf()

happy = nil

end

end

end

local star = display.newImage( “starsmall.jpg” )

star.x = 400

star.y = 100

physics.addBody(star, “static”)

star.collType = “none”

physics.addBody( star, { bounce=10.0, friction=1.0 } )

function movestar()

transition.to(star,{time=900, x=math.random(300,500), y=math.random(0,200), onComplete=movestar})

end

movestar()

function onCollision(event)

–print(“collide!”)

star:removeSelf()

end

Runtime:addEventListener(“collision”, onCollision)

Hi @megabird87,

First thing is that you have two functions named “onCollision”, which you need to correct. Function names need to be unique and, of course, not named something that is reserved by Lua or Corona.

Second, if you want to control which objects register a collision with other objects (but not all), you should configure collision filters. This is described in this guide:

http://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Finally, I suggest that you build a more unified collision handler, instead of having multiple functions to handle individual collisions.

Take care,

Brent

Hi @megabird87,

First thing is that you have two functions named “onCollision”, which you need to correct. Function names need to be unique and, of course, not named something that is reserved by Lua or Corona.

Second, if you want to control which objects register a collision with other objects (but not all), you should configure collision filters. This is described in this guide:

http://docs.coronalabs.com/guide/physics/collisionDetection/index.html

Finally, I suggest that you build a more unified collision handler, instead of having multiple functions to handle individual collisions.

Take care,

Brent