Hello guys,
I have a problem with destroying displayed objects. Now, I will show you what I have in game.
Firstly, objects is spawned when user touch the screen
function spawnEnemy() local enemyNumber = math.random(3) if enemyNumber==1 then redEnemy=display.newImageRect("enemyR.png",20,20) redEnemy.x=math.random(screenWidth) redEnemy.y=math.random(screenHeight) redEnemy.enterFrame=nearBy Runtime:addEventListener("enterFrame", redEnemy) elseif enemyNumber==2 then greenEnemy=display.newImageRect("enemyG.png",20,20) greenEnemy.x=math.random(screenWidth) greenEnemy.y=math.random(screenHeight) greenEnemy.enterFrame=nearBy Runtime:addEventListener("enterFrame", greenEnemy) elseif enemyNumber==3 then blueEnemy=display.newImageRect("enemyB.png",20,20) blueEnemy.x=math.random(screenWidth) blueEnemy.y=math.random(screenHeight) blueEnemy.enterFrame=nearBy Runtime:addEventListener("enterFrame", blueEnemy) end end
Secondly, I used non-physics collision detection (overlapping circles)
function nearBy( self) if ( self == nil ) return false end local dx = self.x - obstacle.x local dy = self.y - obstacle.y local distance = math.sqrt( dx\*dx + dy\*dy ) local objectSize = (self.contentWidth/2) + (obstacle.contentWidth/2) if ( distance \< objectSize ) then transition.to(self, {time=1000, rotation=180, alpha=0}) self:removeSelf() --and here is a problem self=nil end end
ERROR: Runtime error
/Users/dariokarin/Library/Application Support/Outlaw/Sandbox/8/play.lua:133: attempt to perform arithmetic on field ‘x’ (a nil value)
stack traceback:
/Users/dariokarin/Library/Application Support/Outlaw/Sandbox/8/play.lua:133: in function </Users/dariokarin/Library/Application Support/Outlaw/Sandbox/8/play.lua:128>
?: in function <?:169>
And first spawned object can’t be recognizable with non-physics detection. Why is that?
So I need remove that current enemy from display when he is detected. Is there any good way? 
Thanks a lot