Collision not working correctly [VIDEO]

Ok so my collision is prob not set up correctly so its not working correctly… here the video and the code…

Sorry for the bad voice, everyone is sleeping…

https://youtu.be/0X0m8Pk62Zg

and the code

local composer = require( "composer" ) local scene = composer.newScene() local physics = require( "physics" ) function scene:create( event ) local sceneGroup = self.view local BackGround = display.newImageRect( sceneGroup, "BackGround.png", 1080, 1920) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then print("ItsAGo") elseif ( phase == "did" ) then print("ItsAGo2") local P = 1 local floor = display.newImageRect(sceneGroup, "floor.png", 351, 32) floor.x = display.contentWidth \* 0.5 floor.y = 510 physics.addBody( floor, "static", {density = .01, friction = 0.5, bounce = 0 } ) local circle = {} local spawnCircles = function () local FallDown = math.random(display.contentWidth \* 0.2, display.contentWidth \* 0.8 ) circle[P] = display.newImageRect( sceneGroup, "circle.png", 31, 31 ) circle[P].x = FallDown circle[P].y = -70 physics.addBody( circle[P], "dynamic", {density = .01, friction = 0.5, bounce = 0 }) circle[P].gravityScale = 0.1 physics.setGravity( 0, 10 ) circle[P].value = P P = P + 1 if P == 5 then P = 1 end-- I've chosen to reset the counter at 5, if you need more than 5 circles on screen then increase this end myTimer = timer.performWithDelay( 2000, spawnCircles, -1) --1000 is the time function onCollision(event) if( event.phase ~= "began" ) then return true end display.remove(circle[P]) return true end Runtime:addEventListener("collision", onCollision) end end -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

Thanks!

Should I remove the [P]?

try. display.remove( event.target ) if you want to remove the one that hit. However you are likely going to need to put it into a timer to avoid a race condition with the physics engine. Maybe: timer.performWithDelay( 10, function() display.remove( event.target ); end ) Rob

Can you get the code at the top and add and remove the stuff you said? Cause I’m super confused haha

This isn’t that hard:

function onCollision(event)     if( event.phase ~= "began" ) then         return true     end     timer.performWithDelay( 10, function() display.remove( event.target ); end )     return true end

i cant seem to implement the code and make it work… ive tried somethings to fix my problem but they didnt work…

Are you getting errors? 

How is it not working? 

Please provide details.

Rob

Should I remove the [P]?

try. display.remove( event.target ) if you want to remove the one that hit. However you are likely going to need to put it into a timer to avoid a race condition with the physics engine. Maybe: timer.performWithDelay( 10, function() display.remove( event.target ); end ) Rob

Can you get the code at the top and add and remove the stuff you said? Cause I’m super confused haha

This isn’t that hard:

function onCollision(event)     if( event.phase ~= "began" ) then         return true     end     timer.performWithDelay( 10, function() display.remove( event.target ); end )     return true end

i cant seem to implement the code and make it work… ive tried somethings to fix my problem but they didnt work…

Are you getting errors? 

How is it not working? 

Please provide details.

Rob