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! [import]uid: 35210 topic_id: 16442 reply_id: 316442[/import]

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
[lua] event.other:removeSelf()[/lua]
in the listener. [import]uid: 64174 topic_id: 16442 reply_id: 61388[/import]

It is not functioning. Here is what is interacting:

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 )  

and will hit this:

local BarCleaner = display.newRect( 0, (\_h/30), \_w, \_h/30 )  
 BarCleaner:setFillColor( 255, 255, 255, 100 )  
 BarCleaner.isVisible = true -- optional  
 physics.addBody( BarCleaner, "kinematic", { isSensor = true } )  
 BarCleaner:addEventListener( "Collision", function() print("Deleting") end )  

I am not getting the printout of “Deleting” [import]uid: 35210 topic_id: 16442 reply_id: 61406[/import]

Try this
[lua]BarCleaner.collision = function() print(“deleting”) end
BarCleaner:addEventListener(“collision”,BarCleaner)[/lua] [import]uid: 64174 topic_id: 16442 reply_id: 61412[/import]

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. [import]uid: 35210 topic_id: 16442 reply_id: 61422[/import]

For collision to be detected atleast one of the objects must be dynamic. [import]uid: 64174 topic_id: 16442 reply_id: 61426[/import]

Then my next question would be since the chunks have to not be affected by gravity, then how could this work? [import]uid: 35210 topic_id: 16442 reply_id: 61437[/import]

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. [import]uid: 64174 topic_id: 16442 reply_id: 61443[/import]

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

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)  

I have to add a delay onto removing the object because of the nature of the collision detector. This will not work for some reason as the simulator says that “other” is a nil value. [import]uid: 35210 topic_id: 16442 reply_id: 61444[/import]

[lua] eventSub = event.other
timer.performWithDelay(1000, function() eventSub:removeSelf() end,1)[/lua]
[import]uid: 64174 topic_id: 16442 reply_id: 61451[/import]

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

if object.y \< 0 then  
 -- do something like self:removeCode  
end  

?

To reference a specific object, create a table that holds all of your object, so you can address one clearly. Basically you would do something like:

allMyObjects = {}  

to create an empty table, and then create new objects by saying something like

allMyObjects[#allMyObjects+1] = spawnNewObjectCode  

That way you can create your own function on enterFrame that checks which objects have gone out of the screen, like this:

for i, #allMyObjects do  
 if allMyObjects[i].y \< 0 then  
 do something with allMyObjects[i]  
 end  
end  

I am oversimplifying, and you will run into problems if you use my examples literally, but that’s the principle.

[import]uid: 70134 topic_id: 16442 reply_id: 61459[/import]

@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?

[import]uid: 64174 topic_id: 16442 reply_id: 61463[/import]

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

BarCleaner.collision = function(event) eventSub = event.other timer.performWithDelay(1000, function() eventSub:removeSelf() end,1)--[[print("deleting")]] end BarCleaner:addEventListener("collision",BarCleaner) [import]uid: 35210 topic_id: 16442 reply_id: 61488[/import]

sorry, double post! [import]uid: 35210 topic_id: 16442 reply_id: 61666[/import]

Got it fixed:

local function BarCleanerRemove( self , event )   
 print(event.other)  
 timer.performWithDelay(1000, function() event.other:removeSelf() end,1)--[[print("deleting")]]   
 end  
 BarCleaner.collision = BarCleanerRemove  
 BarCleaner:addEventListener("collision",BarCleaner)  

For some stupid reason adding “self” to the parentheses (The name for it is escaping me) worked! [import]uid: 35210 topic_id: 16442 reply_id: 61665[/import]