Removing objects upon touch?

So I’m making a little game and I have multiple instances of the same object on the screen at one time. I want it so that when you tap on an object it removes itself. But right now if I tap on an instance of that object, instead of removing the one I tap on, it removes the most recent instance of it.

The relevant code:

[lua]

–adds asteroids

function newAsteroid1()

    Asteroid1 = display.newImageRect( “Asteroid.png”, 20, 50 )

    Asteroid1.y = display.contentHeight

    Asteroid1.x = math.random( 0, 320 )

    moveAsteroid1()

    Asteroid1:addEventListener( “touch”, onTouchA1 )

end

–removes asteroids

function onTouchA1( event )

    if event.phase == “began” then

        audio.play(explosion, {loops=0} )

        display.remove( Asteroid1 )

        moveSpeed = moveSpeed - 10

        

    end

end

[/lua]

Change line 15 of your code posted above to:

display.remove( event.target )

Thanks! That fixed it.

What if there is no event to remove the object? Say for instance I am trying to remove an object without a user event. I am having the same problem that the most recent one is being removed.

If you want to remove an object without using an event, you have to keep its reference in some variable.    Hard to be more specific without seeing code.   

Change line 15 of your code posted above to:

display.remove( event.target )

Thanks! That fixed it.

What if there is no event to remove the object? Say for instance I am trying to remove an object without a user event. I am having the same problem that the most recent one is being removed.

If you want to remove an object without using an event, you have to keep its reference in some variable.    Hard to be more specific without seeing code.