Best practices for removing display objects off screen?

I’m working on an app with lots of bouncing balls.  Let’s say there are 100 of them.  They’ll move around using the physics APIs, not transition.to().  Some balls will bounce off the screen, and new balls will be added.

I don’t want to leak memory.  Two approaches come to mind:

1.) When a ball is created, store it in a table.  In a game loop, traverse the table and test if the ball has moved off screen by looking at its x,y position.  If it has left the screen, call removeSelf() and then call table.remove() to remove that ball from the table.

2.) Put invisible sensor objects at the display’s borders.  When a collision occurs between a ball and the sensor, remove the ball.  No game loop necessary.

Thoughts?

@Granola,

Either will work, but you could do this too…

The following code will make the object self delete as soon as if moves to the edge of the screen.  (Use wider bounds if necessary.)

Assuming a 320x480 design space:

local function onEnterFrame( self, event ) &nbsp; &nbsp;if( x \< 0 or x \> 320 or y \< 0 or y \< 480) then &nbsp; &nbsp; &nbsp; Runtime:removeEventListener( "enterFrame", self ) &nbsp; &nbsp; &nbsp; display.remove( self ) &nbsp; &nbsp;end end local myBall = display.newCircle( 0,0, 20) myBall.enterFrame = onEnterFrame Runtime:addEventListener( "enterFrame", myBall )&nbsp;

I’d recommend changing the if statement to this:

if( x \< 0 or x \> display.contentWidth or y \< 0 or y \< display.contentHeight) then

That way you don’t need to assume the width/height.

Also it might be worth making the deletion point a little bit further off-screen, otherwise the balls will disappear while they are still half on screen.

local ballRad = 10 if( x \< 0-ballRad or x \> display.contentWidth+ballRad or y \< 0-ballRad or y \< display.contentHeight+ballRad) then

Isn’t it better to just make frame frame bigget then screen, make it physics objects and remove balls when they collide with frame.

There is no better really.  

There is simply what works most easily and most efficiently for your design at the time.  

Different games have different design needs.  

@Granola,

Either will work, but you could do this too…

The following code will make the object self delete as soon as if moves to the edge of the screen.  (Use wider bounds if necessary.)

Assuming a 320x480 design space:

local function onEnterFrame( self, event ) &nbsp; &nbsp;if( x \< 0 or x \> 320 or y \< 0 or y \< 480) then &nbsp; &nbsp; &nbsp; Runtime:removeEventListener( "enterFrame", self ) &nbsp; &nbsp; &nbsp; display.remove( self ) &nbsp; &nbsp;end end local myBall = display.newCircle( 0,0, 20) myBall.enterFrame = onEnterFrame Runtime:addEventListener( "enterFrame", myBall )&nbsp;

I’d recommend changing the if statement to this:

if( x \< 0 or x \> display.contentWidth or y \< 0 or y \< display.contentHeight) then

That way you don’t need to assume the width/height.

Also it might be worth making the deletion point a little bit further off-screen, otherwise the balls will disappear while they are still half on screen.

local ballRad = 10 if( x \< 0-ballRad or x \> display.contentWidth+ballRad or y \< 0-ballRad or y \< display.contentHeight+ballRad) then

Isn’t it better to just make frame frame bigget then screen, make it physics objects and remove balls when they collide with frame.

There is no better really.  

There is simply what works most easily and most efficiently for your design at the time.  

Different games have different design needs.