Can't remove object after it collides with another.

Hello everyone! First of all, I would like to say I am a newbie in lua. I am trying to develop my very first game, and im having trouble managing the collisions between a rectangle and a falling circle (which is an image, i was having trouble with actual circles). After much searching the depths of the internet I found the answer, in parts, because when I tried to apply it to my code it didn’t work. If anyone can help me I would immensely appreciate! 

PS: the rectangle is moving weirdly also, I have no idea whats the matter… Thanks!! :smiley:

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

local roundGroup = display.newGroup()

local rectGroup = display.newGroup()

–Score

score = 0

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

–Rectangle

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

rect:setFillColor(255)

rect.class = “rect”

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

–Rectangle movement

function rect:touch(event)

if event.phase == “began” then

rect.markX = rect.x

elseif event.phase == “moved” then

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

rect.x = x

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

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

end

return true

end

rect:addEventListener( “touch”, rect )

–I’M HAVING TROUBLE IN THIS PART OF THE CODE

–Circle

function SpawnRound()

local round = display.newImage( roundGroup, “round1.png”, math.random(50, 1050), -30)

round.class = “round”

physics.addBody( round, “dynamic”, {density=1.0, friction = 0.5, bounce = 0.1, radius = 30} )

function round:collision(e)

if (e.other.class == “wall”) then

timer.performWithDelay(1, function() round:removeSelf() end, 1)

end

return true

end

return round

end

timer.performWithDelay(500, SpawnRound, 0)

[/lua]

I don’t know why you are having trouble with the collision but I guess “touch focus” would solve your problem with the rectangle.

https://docs.coronalabs.com/api/event/touch/index.html

[lua]

function rect:touch(event)
    if event.phase == “began” then
    rect.markX = rect.x

    display.getCurrentStage():setFocus( self )
    self.isFocus = true
 
    elseif event.phase == “moved” then
        local x = (event.x - event.xStart) + rect.markX
        rect.x = x

        if rect.x > 930 then rect.x = 930 end
        if rect.x < 150 then rect.x = 150 end
        
    elseif event.phase == “ended” then
        display.getCurrentStage():setFocus( nil )
        self.isFocus = nil
    end
    return true
end

[/lua]

I don’t know why you are having trouble with the collision but I guess “touch focus” would solve your problem with the rectangle.

https://docs.coronalabs.com/api/event/touch/index.html

[lua]

function rect:touch(event)
    if event.phase == “began” then
    rect.markX = rect.x

    display.getCurrentStage():setFocus( self )
    self.isFocus = true
 
    elseif event.phase == “moved” then
        local x = (event.x - event.xStart) + rect.markX
        rect.x = x

        if rect.x > 930 then rect.x = 930 end
        if rect.x < 150 then rect.x = 150 end
        
    elseif event.phase == “ended” then
        display.getCurrentStage():setFocus( nil )
        self.isFocus = nil
    end
    return true
end

[/lua]