Remove Objects

I’m wondering how do I remove items on certain height when they fall
I’m trying to do it on this game where balloons fall above and are remove on touched. But I also want them to be removed by itself when they fall on certain height.

Here the code for the balloon I’m saying

–Destroying balloons and adding +1 score
local spawnBall = function( event )
local t = event.target
local phase = event.phase

if “began” == phase then
t:removeSelf() – destroy object
score = score + 1
scoreText.text = score
end
– Stop further propagation of touch event
return true
end
ball:addEventListener( “touch”, spawnBall )

–Balloon is created and at what height and to fall
local balls = {}

local spawnBalloon = function()

ball = display.newImage( “balloon.png” )
ball.x = 60 + math.random( 160 )
ball.y = -100
physics.addBody( ball, { density=0.0, friction=0.1, bounce=0.1 } )
balls[#balls + 1] = ball
end

timer.performWithDelay(500, spawnBalloon, -1)

When I’m trying to get across is how would I remove a falling balloon at a certain

help please :slight_smile: [import]uid: 17058 topic_id: 19250 reply_id: 319250[/import]

make a runtime function for it, and you’d better store your images in display.newGroup rather than only in array
[lua]local function delete()
for i=group.numChildren,1,-1 do

if group[i].y < 0 then
display.remove(group[i])
group[i] = nil
balls[i] = nil – dont know about this part, try it
end
end
end

Runtime:addEventListener(“enterFrame”, delete)[/lua]

i didnt test it, so its up to you [import]uid: 16142 topic_id: 19250 reply_id: 74263[/import]

I tried it and got an error saying

attempt to index global ‘group’ (a nil value) [import]uid: 17058 topic_id: 19250 reply_id: 74266[/import]

how do you instantiate variable group?
You have to create it first, add your display objects to this group and then try to iterate over it. [import]uid: 109453 topic_id: 19250 reply_id: 74267[/import]

it wouldnt work unless you create this:
[lua]local group = display.newGroup()
[lua]and you need to store your images in that group, so function where you create images you write this:
[lua]local spawnBalloon = function()

ball = display.newImage( group, “balloon.png” )[/lua]
or
[lua]local spawnBalloon = function()

ball = display.newImage( “balloon.png” )

group:insert(ball)
[lua] [import]uid: 16142 topic_id: 19250 reply_id: 74269[/import]

Thanks again for the help I was able to get it. Just one last thing I get this in the terminal

WARNING: Attempting to set property(1) with nil

Is that normal to happen I’m just wondering [import]uid: 17058 topic_id: 19250 reply_id: 74275[/import]

if terminal gives you error, than its probably far from normal) [import]uid: 16142 topic_id: 19250 reply_id: 74277[/import]

Well is not a an error is a warning but I fix that anyways thanks for the help [import]uid: 17058 topic_id: 19250 reply_id: 74278[/import]

Hi: i needddd HELPPPPPPPPP:
i have a lot of monsters named equal like this
--------------------CODE-----------------------

function crear_m1 ()
local monstruo = display.newImage(“images/monstruos/monstruo1.png”)
physics.addBody(monstruo, {bounce=0.3, radius = 35, friction=0.5})

local function killm1 (event)
event.target:removeSelf()
end
monstruo:addEventListener (“touch”, killm1)
end
carreta:addEventListener(“touch”, crear_m1)

---------------------DESCRIPTION---------------------
when i touch an element i create monsters, when i touch a monster created it removeSelf But it delete the properties of the other mosnter created so it is a pain…CAN YOU HELP ME PLEASE I AM SO STOCK…
[import]uid: 110762 topic_id: 19250 reply_id: 79772[/import]