Collision isnt registering between 2 objects?

basically i have a game where things are falling form the top of the screen and when it touches player or the bottom platform its supposed to disappear 

it works until i throw it into a loop

im trying to spawn multiple of the grayDot object and i cant figure it out without a loop of some sort so i tried the timer.performWithDelay and it spawns multiple the exact way i want it except it wont register the collision between the 2 objects

but it is detecting collision because it is printing 1 and 2 which i added into the began and ended phase if statements

im just wanting to know how i could get the graydot to dissapear when it touches the player or the platform

and where it says ball in the code is the player

please help

display.setStatusBar( display.HiddenStatusBar ) local physics = require("physics") physics.start() physics.setGravity (0, 25) --Variable local gravity = .5 local background = display.newImageRect("background.png",320, 668 ) background.x = display.contentCenterX background.y = display.contentCenterY local platform = display.newImageRect("Platform.png", 350, 35) platform.x = display.contentCenterX platform.y = display.contentHeight - 2 platform.yScale = display.contentScaleY + 1 physics.addBody(platform, "static", {bounce = .2, friction = 1}) local ball = display.newImageRect("square.png", 50, 50) ball.x = display.contentCenterX ball.y = display.contentCenterY physics.addBody(ball, "dynamic", {bounce = 0, friction = 1}) local gravity = .5 local loop = 0 local numHits = 3 --Functions local function onTouch(event) local ball = event.target local phase = event.phase if ( "began" == phase ) then -- Set touch focus on the ball display.currentStage:setFocus( ball ) -- Store initial offset position ball.touchOffsetX = event.x - ball.x elseif ( "moved" == phase ) then -- Move the ball to the new touch position ball.x = event.x - ball.touchOffsetX if ball.x \< 20 then ball.x = 20 display.currentStage:setFocus(nil) elseif event.x \> 300 then ball.x = 300 display.currentStage:setFocus(nil) end elseif ( "ended" == phase or "cancelled" == phase ) then -- Release touch focus on the ball display.currentStage:setFocus( nil ) end return true end local function onCollision(event) local phase = event.phase if phase == "began" then local obj1 = event.object1 local obj2 = event.object2 if (obj1 == ball and obj2 == grayDot)or (obj1 == grayDot and obj2 == ball) then display.remove(obj2) numHits = numHits - 1 print(3) elseif (obj1 == platform and obj2 == grayDot)or (obj1 == grayDot and obj2 == platform) then display.remove(obj2) print(4) end print(1) elseif phase == "ended" then print(2) end return true end function spawn() grayDot = display.newImageRect("gray\_dot.png", 30, 30) grayDot.x = math.random(20, 300) grayDot.y = -60 physics.addBody(grayDot, "dynamic", {bounce = 0}) grayDot.gravityScale = gravity end -- format and display Runtime:addEventListener("collision", onCollision) ball:addEventListener("touch", onTouch) timer.performWithDelay( 1000, spawn, 0 )

Hi @fluffymonkey69,

I think the issue here is Lua scope and naming conventions. You’re making a global object “grayDot” (not recommended to use globals) and then you’re overwriting that object each time the loop iterates. But then, in your collision detection function, you’re looking up the Lua object “grayDot”, but it will be different each time because you’re overwriting it.

A better approach is to spawn local “grayDot” objects and give them a name property like this:

[lua]

    local grayDot = display.newImageRect(“gray_dot.png”, 30, 30)

    grayDot.name = “grayDot”

[/lua]

And, do the same for your other objects, i.e.:

[lua]

local platform = display.newImageRect(“Platform.png”, 350, 35)

– here…

platform.name = “platform”

local ball = display.newImageRect(“square.png”, 50, 50)

– and here…

ball.name = “ball”

[/lua]

Then, in your collision detection, compare these name properties to each other to determine what collided with what, instead of trying to point to the actual Lua objects. For example:

[lua]

    if (obj1.name == “ball” and obj2.name == “grayDot”) or 

[/lua]

Hope this helps,

Brent

P.S. - IMPORTANT! You also should not scale physics bodies, for example how you’re adjusting the .yScale of the platform. That will scale the display object itself, but the physics body associated with it will be regarded as the core non-scaled body size.

thanks alot for the info and i knew about the scale thing i did it because the object would stop before even touching the image it would be right outside it with a small space in between so i scaled it up to make it look like it was sitting on it and im definitely gonna start using the name thing

Hi @fluffymonkey69,

I think the issue here is Lua scope and naming conventions. You’re making a global object “grayDot” (not recommended to use globals) and then you’re overwriting that object each time the loop iterates. But then, in your collision detection function, you’re looking up the Lua object “grayDot”, but it will be different each time because you’re overwriting it.

A better approach is to spawn local “grayDot” objects and give them a name property like this:

[lua]

    local grayDot = display.newImageRect(“gray_dot.png”, 30, 30)

    grayDot.name = “grayDot”

[/lua]

And, do the same for your other objects, i.e.:

[lua]

local platform = display.newImageRect(“Platform.png”, 350, 35)

– here…

platform.name = “platform”

local ball = display.newImageRect(“square.png”, 50, 50)

– and here…

ball.name = “ball”

[/lua]

Then, in your collision detection, compare these name properties to each other to determine what collided with what, instead of trying to point to the actual Lua objects. For example:

[lua]

    if (obj1.name == “ball” and obj2.name == “grayDot”) or 

[/lua]

Hope this helps,

Brent

P.S. - IMPORTANT! You also should not scale physics bodies, for example how you’re adjusting the .yScale of the platform. That will scale the display object itself, but the physics body associated with it will be regarded as the core non-scaled body size.

thanks alot for the info and i knew about the scale thing i did it because the object would stop before even touching the image it would be right outside it with a small space in between so i scaled it up to make it look like it was sitting on it and im definitely gonna start using the name thing