I’m having an odd issue with the removal of collided objects. I’m creating a series of random falling circles that are removed when they collide with a barrier (much like rain). However, some of the newly spawned objects are removed before they impact the barrier. It think it has to do with the same table index # being assigned twice.
i.e. object at table index 5 is removed on impact with barrier and the index count is reduced to 4 —> a newly generated object fills index 5, but is also simultaneously removed.
---Define variables: local W = display.actualContentWidth local H = display.actualContentHeight local math\_r = math.random display.setStatusBar( display.HiddenStatusBar ) --Start physics: local physics = require("physics") physics.start() --Create table & group to hold circles/spawns: local circles = {} local circle\_group = display.newGroup( ) --Create wall to trigger circle removal: local removeThreshold = display.newRect(W\*0.5, H\*0.5, W, 50) physics.addBody( removeThreshold, { density = 10, friction = 0.5, bounce = 0 } ) removeThreshold.bodyType = "static" removeThreshold.isSensor = true removeThreshold.name = "threshold" removeThreshold.collision = onCollision removeThreshold:addEventListener( "collision", removeThreshold ) --Function for spawning circles: local function spawnCircle() local onCollision = function ( self, event ) if ( event.phase == "began" ) then display.remove(self.tableID[self.index]) self.tableID[self.index] = nil elseif ( event.phase == "ended" ) then end return true end local r = math\_r(25,50); g = math\_r(254,254); b = math\_r(25,50) local circle = display.newCircle( 0, 0, 10 ) circle:setFillColor(r,g,b) circle\_group:insert(circle) circle.x = math\_r(0, W) circle.group = circle\_group circle.name = "circle" circle.collision = onCollision circle:addEventListener( "collision", circle ) physics.addBody(circle, {density=50, bounce=0, radius=30}) circle.bodyType = "dynamic" circle.isSensor = true circle.y = math\_r(-100, -10) circle.tableID = circles circle.index = #circles + 1 circle:setLinearVelocity(0, math\_r(50, 200)) circles[circle.index] = circle return circle end local spawnTimer = timer.performWithDelay(50, spawnCircle, 0)