[Resolved] removing multiple displayobjects that spawned by function

hey all - just a tiny question:

i have this function which makes one new displayobject, imagine a fireball.
i have a timer.performwithdelay to execute that function 10 times which spawns at different random lokation on the screen
my problem is now that - when i try make a function “despawnFireball” like so (see below). it will only remove the newest fireball that spawned. (i suspect there is a really easy solution, i just cant see it, im “new” to Corona so forgive me if its wrong forum or something silly easy, :P)

how do i remove the fireballs which is clicked on and not just the newest one created?

(i tried to do it with tables / arrays without luck but there must be a easier way?) :o

[code]
function spawnFireBall( event )
print(“fireball spawned!”)
newFireBall = display.newCircle( 0, 0, 20 )
newFireBall.x = display.contentWidth * 0.5
newFireBall.y = display.contentHeight * 0.5 - 20
newFireBall:setFillColor(255,0,0)

groupOfFireBall:insert( newFireBall )

randX = math.random (100, display.contentWidth - 100)
randY = math.random (20, display.contentHeight - 20)

– random positions to move to for fireballs
transition.to(newFireBall, {time = 500, x = randX, y = randY})

newFireBall:addEventListener( “touch”, smashFireBall )
end

tmr = timer.performWithDelay(1000,spawnFireBall,5);

function smashFireBall( event )
print(“fireball destroyed!”)
newFireBall:removeSelf();
end

[/code] [import]uid: 163092 topic_id: 30564 reply_id: 330564[/import]

sry the fireball only spawns 5 times ofcause, but same issue, how to remove rest of the “fireballs” when clicked on? :o [import]uid: 163092 topic_id: 30564 reply_id: 122459[/import]

EDIT: Ops, read it wrong. modified code

You can get the reference to the touched object through the event that is passed in. Below is an example.

[lua]function spawnFireBall( event )
print(“fireball spawned!”)
newFireBall = display.newCircle( 0, 0, 20 )
newFireBall.x = display.contentWidth * 0.5
newFireBall.y = display.contentHeight * 0.5 - 20
newFireBall:setFillColor(255,0,0)

groupOfFireBall:insert( newFireBall )

randX = math.random (100, display.contentWidth - 100)
randY = math.random (20, display.contentHeight - 20)

– random positions to move to for fireballs
transition.to(newFireBall, {time = 500, x = randX, y = randY})

newFireBall:addEventListener( “touch”, smashFireBall )
end

tmr = timer.performWithDelay(1000,spawnFireBall,5);

function smashFireBall( event )
print(“fireball destroyed!”)
event.target:removeSelf()
end[/lua] [import]uid: 147305 topic_id: 30564 reply_id: 122469[/import]

hurray it works, it works! /jumps of joy. Thank you so much budershank :slight_smile: i knew it had to be really that simple, easy soiution. i just sat there and starring blind for hours…

Thanks once again :slight_smile:

  • Nemo [import]uid: 163092 topic_id: 30564 reply_id: 122489[/import]

sry the fireball only spawns 5 times ofcause, but same issue, how to remove rest of the “fireballs” when clicked on? :o [import]uid: 163092 topic_id: 30564 reply_id: 122459[/import]

EDIT: Ops, read it wrong. modified code

You can get the reference to the touched object through the event that is passed in. Below is an example.

[lua]function spawnFireBall( event )
print(“fireball spawned!”)
newFireBall = display.newCircle( 0, 0, 20 )
newFireBall.x = display.contentWidth * 0.5
newFireBall.y = display.contentHeight * 0.5 - 20
newFireBall:setFillColor(255,0,0)

groupOfFireBall:insert( newFireBall )

randX = math.random (100, display.contentWidth - 100)
randY = math.random (20, display.contentHeight - 20)

– random positions to move to for fireballs
transition.to(newFireBall, {time = 500, x = randX, y = randY})

newFireBall:addEventListener( “touch”, smashFireBall )
end

tmr = timer.performWithDelay(1000,spawnFireBall,5);

function smashFireBall( event )
print(“fireball destroyed!”)
event.target:removeSelf()
end[/lua] [import]uid: 147305 topic_id: 30564 reply_id: 122469[/import]

hurray it works, it works! /jumps of joy. Thank you so much budershank :slight_smile: i knew it had to be really that simple, easy soiution. i just sat there and starring blind for hours…

Thanks once again :slight_smile:

  • Nemo [import]uid: 163092 topic_id: 30564 reply_id: 122489[/import]