removing offscreen objects

I have a code that starts creating circles after 50 seconds, but since the circles are not removed when they fall off of the screen the app gets really slow after a while.

I tried using the multipuck sample code but it didnt work

here is what i have please help
[lua]local physics = require(“physics”)
physics.start()
physics.setGravity( 10, 0 )

paper = display.newImage( “paper.png” )
function enemymaker(event)
function enemymaker2(event)
ball = display.newImage( “ball.png”,-100,100 )
physics.addBody( ball, { density = 1,friction=0, bounce=.1, radius= 5} )
end
timer.performWithDelay( 100,enemymaker2, 50000 )
end
timer.performWithDelay( 50000,enemymaker, 1 )[/lua] [import]uid: 28068 topic_id: 6907 reply_id: 306907[/import]

please help [import]uid: 28068 topic_id: 6907 reply_id: 24387[/import]

I was having the same issue, try:

ball.isBullet = true

This will make corona check semi-frequently for offscreen objects [import]uid: 12404 topic_id: 6907 reply_id: 24480[/import]

won’t that make the application slow when you are spawning a lot of objects
[import]uid: 28068 topic_id: 6907 reply_id: 24601[/import]

Corona doesn’t automatically delete offscreen objects, you have to take care of this on your own.

@Cowlord: isBullet has nothing to do with offscreen objects. Here is the API description:

A boolean for whether the body should be treated as a “bullet”. Bullets are subject to continuous collision detection, rather than periodic collision detection at world timesteps. This is more computationally expensive, but it prevents fast-moving objects from passing through solid barriers. The default is false.

[import]uid: 5712 topic_id: 6907 reply_id: 24625[/import]

I’m planning to write a demo of culling offscreen objects using a modified version of my hitTest function:

http://developer.anscamobile.com/code/flashs-hittestobject-emulated-using-contentbounds

I’m planning to test just how many objects can be handled in Lua. [import]uid: 12108 topic_id: 6907 reply_id: 24674[/import]

The MultiPuck sample contains code that removes off-screen objects. Of course, you would need to adapt it to your needs.

Tim

local allDisks = {} -- empty table for storing disks, populated by other routine-- Automatic culling of offscreen objectslocal function removeOffscreenItems() for i = 1, #allDisks do local oneDisk = allDisks[i] if (oneDisk.x) then if oneDisk.x < -100 or oneDisk.x > display.contentWidth + 100 or oneDisk.y < -100 or oneDisk.y > display.contentHeight + 100 then oneDisk:removeSelf() end end endendRuntime:addEventListener( "enterFrame", removeOffscreenItems ) -- clean up offscreen disks[/code] [import]uid: 8196 topic_id: 6907 reply_id: 24717[/import]

thanks for the help
it turn out i just had to use a table
:stuck_out_tongue: [import]uid: 28068 topic_id: 6907 reply_id: 24986[/import]

A nice way of setting up event-driven onscreen/offscreen detection is to define the entire screen as a Sensor (ie, add an invisible rectangle that fills the screen and set isSensor to true), then you can listen for events as each object enters and leaves the screen.

I imagine this has much better performance (especially for large numbers of objects) than polling every object, every frame, at the application level. Plus, it’s far more accurate, as the above MultiPuck code only tests the object’s (x,y) point, whereas with true collision detection you are testing across the entire object.

Here is sample code:

[lua]-- init physics
require ‘physics’
physics.start()
physics.setGravity( 0, 0 )

– add “onscreen” sensor
local sensor = display.newRect( 0, 0, display.contentWidth-1, display.contentHeight-1 )
physics.addBody( sensor, ‘static’ )
sensor.isSensor = true
sensor.alpha = 0.3
sensor.name = “sensor”

– toss a spinning box through
local box = display.newRect( -250, 150, 150, 100 )
physics.addBody( box )
box.angularVelocity = 40
box:setLinearVelocity( 100, 0 )
box.name = “box”

– collision event listener
function onCollision( event )
print ('collision: ', event.phase, event.object1.name, event.object2.name )
end

– add listener
Runtime:addEventListener( “collision”, onCollision )[/lua] [import]uid: 70530 topic_id: 6907 reply_id: 44612[/import]