Removing Objects on Collision

OK Hope you all had a great time. So my problem is how would I remove an object on collision. Like this is what I mean is a level there's three stars the goal is for the ball to collect the three stars that what I mean about collision if that considered collision.

Help is appreciated :)

correct if I'm wrong but this would be right for how I would remove the stars

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
local star = display.newRect(-20,0, 100, 100)
star:setFillColor(0, 255, 0)
physics.addBody (star, "static" )
star.name = "star"
 
local function onCollision(event)
 
if event.phase == "began" and event.other.name == "star" then
display.remove(event.other)
score = score + math.random( 10 )
score_txt.text = "Score: ".. score
end
end
 
ball.preCollision = onLocalPreCollision
ball:addEventListener("collision", onCollision)

Once you have your collision function, just add the name of your object. In this circumstance it's called object.

1
2
3
4
5
6
7
8
-- removing an object, removes it forever:
object:removeSelf()
 
-- If you would like to just remove the appearance of the object you could do this:
object.isVisible = false
 
-- Taking away the physics of the object just in case ;): 
object.isBodyActive = false

Works!! I was looking for this in the online docs but the samples there don't work.
It says the first object should be event.object1 and the second should be event.object2. I tried it and it gives me a nil error.
Now with your example it just works great!
thanks for sharing

@adrianeraldo yeah your welcome glad this help :)

views:1715 update:2012/2/9 11:37:26
corona forums © 2003-2011