How can remove an object after it collides with another?

So i´ve been trying to code my first game! I have random spawning circles and a rectangle. I want to make the circles to disappear after they collide with the rectangle. Can anyone help me? Thanks!

Can you post some code that we can build off of? Thanks!

Edit: ** Corrected placement of return in function **

Agreed, understanding how you make your collision handler would help.  

That said, I always use table listeners and would do it this way:

-- Answer is pseudo-code mixed with actual code... local circ = display.newCircle( ... ) ... function circ.collision( self, event ) if( event.phase == "began" ) then self:removeEventListener("collision") timer.performWithDelay( 1, function() display.remove(self) end ) return false end end circ:addEventListener( "collision" )

So this is the full code:

[lua]

local physics = require(“physics”)

physics.start()

halfW = display.contentWidth*0.5

halfH = display.contentHeight*0.5

–Background

local bkg = display.newImage(“bkg.png”, halfW, halfH)

–Score

score = 0

scoreText = display.newText(score, halfW, 260, native.systemFont, 150)

–Rectangle

rect = display.newRect( 540, 1800, 300, 75 )

rect:setFillColor(255)

rect.myName = “rect”

physics.addBody(rect, “static”, {density=1.0, friction = 0.5, bounce = 0.0} )

–Rectangle movement

function rect:touch(event)

if event.phase == “began” then

self.markX = self.x

elseif event.phase == “moved” then

local x = (event.x - event.xStart) + self.markX

self.x = x

if self.x > 930 then self.x = 930 end

if self.x < 150 then self.x = 150 end

end

return true

end

rect:addEventListener( “touch”, rect )

–circle

local function SpawnCircle()

circle = display.newCircle ( math.random(30,1890), 0, 30)

circle:setFillColor(1)

physics.addBody( circle, “dynamic”,{density=1.0, friction = 0.5, bounce = 0} )

end

timer.performWithDelay(1000, SpawnCircle, 0)

[/lua]

I want to make so that the ball falling hit the rectangle and disappear, and when a single ball hit the rectangle, the score ups in one point. can anyone help me? I’ve been stuck for days…

Hi @pedromaschio.shin,

In your code, I don’t see any type of collision detection included. Did you look at the response by @roaminggamer in this thread?

Brent

Can you post some code that we can build off of? Thanks!

Edit: ** Corrected placement of return in function **

Agreed, understanding how you make your collision handler would help.  

That said, I always use table listeners and would do it this way:

-- Answer is pseudo-code mixed with actual code... local circ = display.newCircle( ... ) ... function circ.collision( self, event ) if( event.phase == "began" ) then self:removeEventListener("collision") timer.performWithDelay( 1, function() display.remove(self) end ) return false end end circ:addEventListener( "collision" )

So this is the full code:

[lua]

local physics = require(“physics”)

physics.start()

halfW = display.contentWidth*0.5

halfH = display.contentHeight*0.5

–Background

local bkg = display.newImage(“bkg.png”, halfW, halfH)

–Score

score = 0

scoreText = display.newText(score, halfW, 260, native.systemFont, 150)

–Rectangle

rect = display.newRect( 540, 1800, 300, 75 )

rect:setFillColor(255)

rect.myName = “rect”

physics.addBody(rect, “static”, {density=1.0, friction = 0.5, bounce = 0.0} )

–Rectangle movement

function rect:touch(event)

if event.phase == “began” then

self.markX = self.x

elseif event.phase == “moved” then

local x = (event.x - event.xStart) + self.markX

self.x = x

if self.x > 930 then self.x = 930 end

if self.x < 150 then self.x = 150 end

end

return true

end

rect:addEventListener( “touch”, rect )

–circle

local function SpawnCircle()

circle = display.newCircle ( math.random(30,1890), 0, 30)

circle:setFillColor(1)

physics.addBody( circle, “dynamic”,{density=1.0, friction = 0.5, bounce = 0} )

end

timer.performWithDelay(1000, SpawnCircle, 0)

[/lua]

I want to make so that the ball falling hit the rectangle and disappear, and when a single ball hit the rectangle, the score ups in one point. can anyone help me? I’ve been stuck for days…

Hi @pedromaschio.shin,

In your code, I don’t see any type of collision detection included. Did you look at the response by @roaminggamer in this thread?

Brent