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”)