Delete a self object of a function [corona sdk]

I have a little problem and I’m searching for an easy solution,
in my game, if a bullet touches a specific ennemy, this target should be deleted and respawn otherwhere,
I’m using the self-collision event to make the instructions single to each ennemy,
the problem is that self-collision function only works if he recognise the target, but as I delete it the first time in my collision function, that ennemy doesn’t exist anymore for my function. It works only the first time.
I hope that my problem is understandable,
here is an example of the code:
 

local ennemy ennemy = display.newRect(0,0, 20, 50) transition.to( ennemy, {time = 2000, x = 240, y = 160} ) local function onCollision(self,event) display.remove( bullet ) display.remove( ennemy ) ennemy = display.newRect(0,0, 20, 50) transition.to( ennemy, {time = 2000, x = 240, y = 160} ) end ennemy.collision = onCollision ennemy:addEventListener( "collision", ennemy )

When you assign a new object to a variable it basically overwrites the previous one.

Then correct way of  doing this would be:

 local ennemy ennemy = display.newRect(0,0, 20, 50) transition.to( ennemy, {time = 2000, x = 240, y = 160} ) local function onCollision(self,event) display.remove( bullet ) ennemy:removeEventListener("collision") --just best practice display.remove( ennemy ) ennemy = display.newRect(0,0, 20, 50) ennemy.collision = onCollision --recreate the event listener ennemy:addEventListener( "collision", ennemy ) transition.to( ennemy, {time = 2000, x = 240, y = 160} ) end ennemy.collision = onCollision ennemy:addEventListener( "collision", ennemy )

FANTASTIC!

Thanks alot! It seems legit as solution! I like it!

Keep answering topics ;D you’re useful

local thanks = display.thanks \* 1000

When you assign a new object to a variable it basically overwrites the previous one.

Then correct way of  doing this would be:

 local ennemy ennemy = display.newRect(0,0, 20, 50) transition.to( ennemy, {time = 2000, x = 240, y = 160} ) local function onCollision(self,event) display.remove( bullet ) ennemy:removeEventListener("collision") --just best practice display.remove( ennemy ) ennemy = display.newRect(0,0, 20, 50) ennemy.collision = onCollision --recreate the event listener ennemy:addEventListener( "collision", ennemy ) transition.to( ennemy, {time = 2000, x = 240, y = 160} ) end ennemy.collision = onCollision ennemy:addEventListener( "collision", ennemy )

FANTASTIC!

Thanks alot! It seems legit as solution! I like it!

Keep answering topics ;D you’re useful

local thanks = display.thanks \* 1000