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 :slight_smile: [import]uid: 17058 topic_id: 19951 reply_id: 319951[/import]

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

[code]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)[/code]

I found in one of my games so please anybody just tell me if this is correct.

Thanks [import]uid: 17058 topic_id: 19951 reply_id: 77634[/import]

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

[code]

– 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 [import]uid: 23689 topic_id: 19951 reply_id: 77687[/import]

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
[import]uid: 12407 topic_id: 19951 reply_id: 82303[/import]

@adrianeraldo yeah your welcome glad this help :slight_smile: [import]uid: 17058 topic_id: 19951 reply_id: 82309[/import]

Doing this :

-- Taking away the physics of the object just in case ;): object.isBodyActive = false on Collision returns the following error :

13/01/13 17:28:33,614 Corona Simulator: ERROR: display object property isBodyActive cannot be set when the world is locked and in the middle of number crunching, such as during a collision event

:frowning: [import]uid: 9328 topic_id: 19951 reply_id: 138700[/import]

Doing this :

-- Taking away the physics of the object just in case ;): object.isBodyActive = false on Collision returns the following error :

13/01/13 17:28:33,614 Corona Simulator: ERROR: display object property isBodyActive cannot be set when the world is locked and in the middle of number crunching, such as during a collision event

:frowning: [import]uid: 9328 topic_id: 19951 reply_id: 138700[/import]