Hello everybody, I am newer to Corona, and I’ve recently been messing around with some spawning and collision code. The issue that I am having is that when I try to remove the spawned objects upon collision, the object:removeSelf() returns the value of ‘nil’. Two different tries at this code are posted below and I get the same thing for both.
--Spawning multiple bananas in random locations local function spawnObjects() local bananas = display.newImageRect("object\_bananas.png", 30, 20); physics.addBody(bananas, "dynamic", {radius = 11}); bananas.x = math.random(0, 320); bananas.y = -40; transition.to( bananas, {time = math.random(6000, 10000), x = math.random(10, 310) , y = 600, }); -- function to handle the collision on the bananas function bananas:collision(e) -- only perform logic when the bananas are colliding with monkey if (e.other.class == "monkey") then -- cannot remove objects during a collision, so wait a short moment for it to end timer.performWithDelay(1, function() -- remove the bananas bananas:removeSelf() end, 1) end -- always return true because we have handled the collision return true end -- attach a collision listener to the bananas bananas:addEventListener("collision",bananas) return bananas end local total\_bananas = 15 tmr = timer.performWithDelay(2000, spawnObjects, total\_bananas); ALTERNATIVE local bananas -- function to handle the collision on the bananas local function onCollision(bananas, event) -- only perform logic when the bananas are colliding with monkey if (event.other.class == "monkey") then -- cannot remove objects during a collision, so wait a short moment for it to end timer.performWithDelay(1, function() -- remove the bananas bananas:removeSelf() end, 1) end -- always return true because we have handled the collision return true end --Spawning multiple bananas in random locations local function spawnObjects() local bananas = display.newImageRect("object\_bananas.png", 30, 20); physics.addBody(bananas, "dynamic", {radius = 11}); bananas.x = math.random(0, 320); bananas.y = -40; transition.to( bananas, {time = math.random(6000, 10000), x = math.random(10, 310) , y = 600, }); bananas.collision = onCollision bananas:addEventListener("collision", bananas) sceneGroup:insert(bananas) return bananas end