When I spawn an object and tap it to destory it only destroys the newest object

Hey guys so my game is setup so when objects spawn you tap them to get points and when you tap it destroys itself but when their is more then one it only destroys the newest one.

Any Help Thanks!

local composer = require( "composer" ) local scene = composer.newScene() system.activate( "multitouch" ) -- include Corona's "physics" library local physics = require "physics" physics.start(); physics.pause() -------------------------------------------- -- forward declarations and other locals local screenW, screenH, halfW = display.contentWidth, display.contentHeight, display.contentWidth\*0.5 function scene:create( event ) -- Called when the scene's view does not exist. -- -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc. local score = require("score") local sceneGroup = self.view local earth = display.newImage("Earth.png",415,80) earth:scale(0.15,0.15) local moon = display.newImage("moon.png",55,250) moon:scale(0.15,0.15) local numberSmiles = 1 --local variable; amount can be changed local scoreText = score.init({ fontSize = 20, font = "Helvetica", x = display.contentCenterX, y = 20, maxDigits = 7, leadingZeros = false, filename = "scorefile.txt", }) scoreText:setFillColor(1,0,0) local numberSmiles = 1 --local variable; amount can be changed local function spawnSmiles() for i=1,numberSmiles do smile = display.newImageRect("rocket.png", 45, 45); -- smile:setReferencePoint(display.CenterReferencePoint); --not necessary; center is default smile.x = math.random(-10, 400); smile.y = math.random(55,250) --transition.to( smile, { time=math.random(2000,8000), x=earth.x , y=earth.y, } ); end local function rocketdestroyed( event ) if (event.phase == "began") then score.add(event.target.value) smile:removeSelf() end end smile:addEventListener("touch",rocketdestroyed) end smile = nil timer.performWithDelay( 1000, spawnSmiles, 0 ) --fire every 1 seconds end

I have got it partway solved! I added this function

function destroyrocket( event ) if (smile.removeSelf ~= nil) then smile:removeSelf() score.add(event.target.value) end end smile:addEventListener("touch",destroyrocket)

This is because, the way you set up your spawning function, each time you spawn an object you are over-writing the reference to the previous object. You are going to need to spawn objects into a table. Take a look at the below blogpost. It’s an oldie but a goodie, and discusses exactly what you’re trying to achieve here:

https://coronalabs.com/blog/2011/09/14/how-to-spawn-objects-the-right-way/

@Alex@Panc

I do not get this tutorial? I keep getting an error can you make a working example? Thanks Tyler

Here’s a blogpost I did a while ago with working examples showing control over individual spawned objects:

http://www.panc.co/code/corona-sdk-beginner-tutorial-series-entry-4

I have got it partway solved! I added this function

function destroyrocket( event ) if (smile.removeSelf ~= nil) then smile:removeSelf() score.add(event.target.value) end end smile:addEventListener("touch",destroyrocket)

This is because, the way you set up your spawning function, each time you spawn an object you are over-writing the reference to the previous object. You are going to need to spawn objects into a table. Take a look at the below blogpost. It’s an oldie but a goodie, and discusses exactly what you’re trying to achieve here:

https://coronalabs.com/blog/2011/09/14/how-to-spawn-objects-the-right-way/

@Alex@Panc

I do not get this tutorial? I keep getting an error can you make a working example? Thanks Tyler

Here’s a blogpost I did a while ago with working examples showing control over individual spawned objects:

http://www.panc.co/code/corona-sdk-beginner-tutorial-series-entry-4