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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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 } )

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
-- 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
views:1663 update:2011/10/1 9:04:19
corona forums © 2003-2011