I have a display object that drops down my screen. I’d like to remove it once it drops past a specified y value. What’s the best way to detect when it’s past the threshold I’d like to set? (I want to remove these objects that drop off the bottom of the screen to free up memory)
I thought I could check its y location each frame but I’d like the check to be associated with the object and not the Runtime so that I don’t have to manage the addition and removal of a Runtime listener (so that when I remove and nil the object the listener goes too).
Is there any sort of “enterFrame” listener I can associate with the object it’s self to check it’s y value? How else could I do this elegantly? I’m not sure what approach to take.
Currently I’m using a timer.performWithDelay() to remove and nil out each object after a set time. This works but seems kind of like a clunky way to do this. Also, it means I ought to keep track of my timer.performWithDelay() functions in case I leave the scene before they’re all complete.
There are two good ways of doing this. The first example is using a collision listener to see what hits it and destroys them.
The second is on a runtime listener. You dont need to use a runtime listener for every object, just deal with all objects in the one runtime listener. Just remember to remove this runtime listener when exiting scene.
Here is the 2 sets of code to show you how it can be used.
Collision Listener ( best option)
display.setStatusBar(display.HiddenStatusBar) physics = require("physics") physics.start() -- set y threshold level local threshold = 300 local box = {} local numbox = 5 for i=1,numbox do box[i] = display.newRect(i\*50,50,40,40) physics.addBody(box[i], {density=1}) box[i].gravityScale = i\*0.2 -- set varying gravity end local colsensor = display.newRect(0,threshold,320,20) -- collision sensor bar physics.addBody(colsensor, "static", {isSensor = true}) local function hitsensor(self,e) if e.phase == "began" then display.remove(e.other) e.other = nil end end colsensor.collision = hitsensor colsensor:addEventListener("collision")
Runtime Listener:
display.setStatusBar(display.HiddenStatusBar) physics = require("physics") physics.start() -- set y threshold level local threshold = 300 local box = {} local numbox = 5 for i=1,numbox do box[i] = display.newRect(i\*50,50,40,40) physics.addBody(box[i], {density=1}) box[i].gravityScale = i\*0.2 -- set varying gravity end local function checkthreshold(e) for i=1,numbox do if box[i] then -- checks to see if box is not nil if box[i].y \> threshold then -- remove box if greater than threshold display.remove(box[i]) box[i] = nil end end end end Runtime:addEventListener("enterFrame", checkthreshold)
There are two good ways of doing this. The first example is using a collision listener to see what hits it and destroys them.
The second is on a runtime listener. You dont need to use a runtime listener for every object, just deal with all objects in the one runtime listener. Just remember to remove this runtime listener when exiting scene.
Here is the 2 sets of code to show you how it can be used.
Collision Listener ( best option)
display.setStatusBar(display.HiddenStatusBar) physics = require("physics") physics.start() -- set y threshold level local threshold = 300 local box = {} local numbox = 5 for i=1,numbox do box[i] = display.newRect(i\*50,50,40,40) physics.addBody(box[i], {density=1}) box[i].gravityScale = i\*0.2 -- set varying gravity end local colsensor = display.newRect(0,threshold,320,20) -- collision sensor bar physics.addBody(colsensor, "static", {isSensor = true}) local function hitsensor(self,e) if e.phase == "began" then display.remove(e.other) e.other = nil end end colsensor.collision = hitsensor colsensor:addEventListener("collision")
Runtime Listener:
display.setStatusBar(display.HiddenStatusBar) physics = require("physics") physics.start() -- set y threshold level local threshold = 300 local box = {} local numbox = 5 for i=1,numbox do box[i] = display.newRect(i\*50,50,40,40) physics.addBody(box[i], {density=1}) box[i].gravityScale = i\*0.2 -- set varying gravity end local function checkthreshold(e) for i=1,numbox do if box[i] then -- checks to see if box is not nil if box[i].y \> threshold then -- remove box if greater than threshold display.remove(box[i]) box[i] = nil end end end end Runtime:addEventListener("enterFrame", checkthreshold)