How can I remove an object once it leaves the screen?

i want this crate object to be removed once its x axis equals 0 or when it completely leaves the screen, how can i do this? thanks

[code]
local crate = display.newImage( “safex3x3.png” )
crate.x = 15
crate.y = 110
crate.rotation = 0
localGroup:insert(crate)

local function killCrate (event)
audio.play( destroySound )
if crate.x == 0 then
crate:removeSelf()
lives = lives - 1
livesDisplay.text = lives
if lives == 0 then
timerInfo = timer.performWithDelay(3000, changeScene, 1)
end
end
end
physics.addBody( crate, { density=3.0, friction=1.5, bounce=0.1 } )
[/code] [import]uid: 10827 topic_id: 10104 reply_id: 310104[/import]

Not sure if you are actually intending to remove the object when it leaves the X-axis (horizontal) or when it falls out the bottom on the Y-axis (vertical), at any rate a good approach for this might be to go the “samarai fruit” pattern route from the example code and create a rectangle to act as a platform that catches objects and disposes of them.

Again, just tailor according to the correct axis, if you want to catch stuff that falls off the bottom of the screen and remove it you can do something like this:

Look this up in the Samarai Fruit example and it should get you going in the right direction.

-- Adding a collision filter so the crates do not collide with each other, they only collide with the catch platform  
local crate = {density = 1.0, friction = 0.3, bounce = 0.2, filter = {categoryBits = 2, maskBits = 1}}  
local catchPlatformProp = {density = 1.0, friction = 0.3, bounce = 0.2, filter = {categoryBits = 1, maskBits = 2}}  
-- Create a platform at the bottom to "catch" the crate and remove it  
function setUpCatchPlatform()  
  
 local platform = display.newRect( 0, 0, display.contentWidth \* 4, 50)  
 platform.x = (display.contentWidth / 2)  
 platform.y = display.contentHeight + display.contentHeight  
 physics.addBody(platform, "static", catchPlatformProp)  
  
 platform.collision = onCatchPlatformCollision  
 platform:addEventListener( "collision", platform )  
end  
  
function onCatchPlatformCollision(self, event)  
 -- Remove the object that collided with the platform  
 event.other:removeSelf()  
end  
  

Last thing to do is just to call SetUpCatchPlatform() when you are setting up your level…

Now, everytime a crate falls out the bottom and hits the platform it will get destroyed.

Good luck!

Croisened

[import]uid: 48203 topic_id: 10104 reply_id: 36887[/import]