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 :)

make a runtime function for it, and you'd better store your images in display.newGroup rather than only in array

1
2
3
4
5
6
7
8
9
10
11
12
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)

I tried it and got an error saying

attempt to index global 'group' (a nil value)

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.

it wouldnt work unless you create this:

1
2
3
4
5
6
7
8
9
10
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" )

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

if terminal gives you error, than its probably far from normal)

Well is not a an error is a warning but I fix that anyways thanks for the help

views:1299 update:2011/12/30 9:10:41
corona forums © 2003-2011