Collision Filtering with catergoryBits and maskBits

I already got to the point to understand how this system works, 
 
but I don’t know how to set specific functions for each collision. So I have three objects in my project, a ball, a tree and a score, all are images. The ball can collide with the tree and the score image, when it collides with a tree the game should be over, when it collides with the score then the score should go up by 2. 
 
With the solution I should be able to let the ball collide with multiple trees and score images, I have 20 of each on different random positions.
 
Anyone can help me?

Heres my code :

function scoreCollision( event )

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

    – SCORE + 2

  end

end

Runtime:addEventListener(“collision”, scoreCollision)

function treeCollision( event )

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

    – GAME OVER

  end

end

Runtime:addEventListener(“collision”, treeCollision)

ballCollisionFilter = { categoryBits=1, maskBits=6 }

treeHitCollisionFilter = { categoryBits=2, maskBits=1}

scoreCollisionFilter = { categoryBits=4, maskBits=1 }

ball1 = display.newImage(…)

physics.addBody(ball1, “dynamic”, {friction=0, bounce=0, filter=ballCollisionFilter})

ball1.collision = scoreCollision

ball1.collision = treeCollision

ball1:addEventListener(“collision”)

treeHit = display.newImage(…)

physics.addBody(treeHit, {friction=0, bounce=0, filter=treeHitCollisionFilter})

treeHit.collision = treeCollision

treeHit:addEventListener(“treeCollision”)

score = display.newImage(…)

physics.addBody(score, {friction=0, bounce=0, filter=scoreCollisionFilter})

score.collision = scoreCollision 

score:addEventListener(“collision”)

Your code has some issues.

The function signature of your listener is wrong for the way you’re using it.  i.e. You are using it as a Runtime listener and a table listener, but it has the signature of a Runtime listener.

I also see you’re assigning the collision field twice which is… wrong… you can only assign one value to a field.

I noticed you seem to be using globals too, which is a bad practice.  That said, I’ll let you worry about that.

I also don’t understand why you are adding a listener to the score object… but I assume you have to collide with them to gain score?

I also don’t know what object is the player equivalent so I’ll assume the ball is the player.  This is how I would implement your collision listener.

local ball1 = display.newImage(...) -- ONLY add listener to ball (player) object. function ball1.collision( self, event ) local isA = event.other.isA if(event.phase ~= "began") then return false end if( isA == "tree" ) then -- GAME OVER CODE elseif( isA == "score" ) then -- SCORE INCREMENT CODE end return false end ball1:addEventListener( "collision" ) -- when making trees and scores add an 'isA' field... local tree = display.newImage(...) tree.isA = "tree" local score = display.newImage(...) score.isA = "score"

Your code has some issues.

The function signature of your listener is wrong for the way you’re using it.  i.e. You are using it as a Runtime listener and a table listener, but it has the signature of a Runtime listener.

I also see you’re assigning the collision field twice which is… wrong… you can only assign one value to a field.

I noticed you seem to be using globals too, which is a bad practice.  That said, I’ll let you worry about that.

I also don’t understand why you are adding a listener to the score object… but I assume you have to collide with them to gain score?

I also don’t know what object is the player equivalent so I’ll assume the ball is the player.  This is how I would implement your collision listener.

local ball1 = display.newImage(...) -- ONLY add listener to ball (player) object. function ball1.collision( self, event ) local isA = event.other.isA if(event.phase ~= "began") then return false end if( isA == "tree" ) then -- GAME OVER CODE elseif( isA == "score" ) then -- SCORE INCREMENT CODE end return false end ball1:addEventListener( "collision" ) -- when making trees and scores add an 'isA' field... local tree = display.newImage(...) tree.isA = "tree" local score = display.newImage(...) score.isA = "score"