Destroy spawned objects on touch

Hi, I want destroy spawned objects (that spawn every 1 sec randomly in the screen) touching them.

I looked around the web for a solution but nobody has asked a thing like this.

This is my code: (thunder is my object that spawns every 1 sec and I want to destroy when I touch him)

With this code the first object that spawns I can destroy him but the others no.

local thunder = display.newImageRect( "thunder\_icon.png",100,100) thunder.x = larghezza / 2 thunder.y = altezza / 2 local spawnTimer local function myToccoListener(event) display.remove(thunder) return true end thunder:addEventListener("touch", myToccoListener) local spawn = function() local xCoord = math.random(display.contentWidth \* 0, display.contentWidth \* 1.0) local thunder = display.newImageRect( "thunder\_icon.png",100,100) thunder.x = xCoord thunder.y = 50 end spawnTimer = timer.performWithDelay(1000, spawn, -1)

There are several problems with your design:
.

  1. You are creating your thunder object at the beginning of your code and assigning it to a variable called thunder. Scope of this thunder variable is global to your module.
    .
  2. Your spawn function creates a new object and assigns it to a new variable called thunder. Scope of this thunder variable is local to the spawn function only.
    .
  3. The myToccoListener function is used as a listener function for the global thunder object only, so only that object will be removed when touched.
    .
  4. The myToccoListener function will try to remove the global thunder object 2 or more times, once when event.phase is “began”, potentially many times if event.phase is “moved”, and once when event.phase is “ended”.
    .
  5. None of the thunder objects created in the spawn function have touch event listeners, so none of them will do anything when tapped.
    .
    Hopefully this tutorial will help: https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Thank you very much! It worked perfectly!

I changed the code like this:

local spawnTimer local spawn = function() local xCoord = math.random(display.contentWidth \* 0, display.contentWidth \* 1.0) local thunder = display.newImageRect( "thunder\_icon.png",100,100) thunder.x = xCoord thunder.y = 50 local function myToccoListener(event) display.remove(thunder) return true end thunder:addEventListener("touch", myToccoListener) end spawnTimer = timer.performWithDelay(1000, spawn, -1)

There are several problems with your design:
.

  1. You are creating your thunder object at the beginning of your code and assigning it to a variable called thunder. Scope of this thunder variable is global to your module.
    .
  2. Your spawn function creates a new object and assigns it to a new variable called thunder. Scope of this thunder variable is local to the spawn function only.
    .
  3. The myToccoListener function is used as a listener function for the global thunder object only, so only that object will be removed when touched.
    .
  4. The myToccoListener function will try to remove the global thunder object 2 or more times, once when event.phase is “began”, potentially many times if event.phase is “moved”, and once when event.phase is “ended”.
    .
  5. None of the thunder objects created in the spawn function have touch event listeners, so none of them will do anything when tapped.
    .
    Hopefully this tutorial will help: https://coronalabs.com/blog/2015/06/16/tutorial-scope-for-beginners/

Thank you very much! It worked perfectly!

I changed the code like this:

local spawnTimer local spawn = function() local xCoord = math.random(display.contentWidth \* 0, display.contentWidth \* 1.0) local thunder = display.newImageRect( "thunder\_icon.png",100,100) thunder.x = xCoord thunder.y = 50 local function myToccoListener(event) display.remove(thunder) return true end thunder:addEventListener("touch", myToccoListener) end spawnTimer = timer.performWithDelay(1000, spawn, -1)