Event for when an object leaves the screen

Hello-

I have read the API on how to create your own events, but it was kind of confusing to me, so I am coming here for help.

I need an event for when an object leaves the screen (Leaves through the top of the screen to be exact) and I am not sure how to set up an event in order the be able to reference the object that left the screen as event.target.~whatever~

The reason I need this is because I am creating many objects with the same name through a timer. I need to be able to remove a specific one, hence the need for event.target (or something of the like)

Thanks and hope you can help me!

If what you want is to remove any object that leaves the screen, just keep a sensor on the top of a screen and have a collision listener for the sensor. Remove the colliding object using
 event.other:removeSelf()
in the listener.

It is not functioning. Here is what is interacting:

1
2
3
4
5
6
7
8
9
10
local leftChunk = display.newImage("block2.png",0,(_h/10)*9)
                leftChunk:scale( (_w/20) / leftChunk.height , (_w/20) / leftChunk.height  )
                leftChunk:setReferencePoint(display.TopRightReferencePoint)
                leftChunk.x = _w - rightValue
                leftChunk.y = (_h/20)*19
                --physics.addBody( leftChunk, { density = 1, friction = 0, bounce = 0 } )
                physics.addBody( leftChunk, "kinematic", { friction=0, bounce=0} )
                leftChunk.side = "left"
                leftChunk.bodyType = "kinematic"
                leftChunk:setLinearVelocity( 0 , -20 )

Try this

1
2
BarCleaner.collision = function() print("deleting") end
BarCleaner:addEventListener("collision",BarCleaner)

No, still not working... Its like the two objects wont interact at all, even when it is not a sensor and they are both kinematic.

For collision to be detected atleast one of the objects must be dynamic.

Then my next question would be since the chunks have to not be affected by gravity, then how could this work?

I can think of 2 ways:

1.Keep the sensor as dynamic, and update the position of the sensor in every frame using Runtime:addEventListener("enterFrame")

2.Keep the sensor as dynamic, and create a static sensor object with the same dimensions as that of the sensor. Now use some joints to join the two objects. This will be more efficient.

Ok, I figured that out, but now I have another-

1
2
3
4
5
6
7
8
9
function BarCleanerRemove(eventSub)
                print("deleting") 
                eventSub.other:removeSelf()
        end 
        
        BarCleaner.collision = function(event) 
                eventSub = event
                timer.performWithDelay(1000, BarCleanerRemove(eventSub))--[[print("deleting")]] end
        BarCleaner:addEventListener("collision",BarCleaner)

1
2
 eventSub = event.other
 timer.performWithDelay(1000, function() eventSub:removeSelf() end,1)

Sheesh! As a hardcore math-fanatic, as opposed to using dynamics, all I can think is: how hard can it be to say

1
2
3
if object.y < 0 then
  -- do something like self:removeCode
end

@thomas6 If you have a large number of objects, isn't checking each object's position every frame tedious?
Having a sensor is more efficient,right?

Now it says that it is attempting to index upvalue eventSub??

1
2
3
4
BarCleaner.collision = function(event) 
                eventSub = event.other
                timer.performWithDelay(1000, function() eventSub:removeSelf() end,1)--[[print("deleting")]] end
        BarCleaner:addEventListener("collision",BarCleaner)
views:1573 update:2011/10/19 14:58:09
corona forums © 2003-2011