Removing object at certain position

Hi,

I was wondering if it was possible to have my object be removed after it hits a certain position on the screen (x330, y250).

I have been looking into replacing the object with a “nil” value, however have been unsure as to how to make it execute removal at a certain axis position.

If anybody can point me in the right direction that would be great.

Thanks!

You could try something like 

local object=display.newCircle(10,100,100) local myX=330;local myY=250; local function removeObject(event) if object.x==myX and object.y==myY then object:removeSelf();object=nil; Runtime:removeEventListener("enterFrame",removeObject) end end Runtime:addEventListener("enterFrame",removeObject)

Remember that runtime listeners keep running even after the function executes because they are global. So assuming you are done after a single execution of the function, you should remove the runtime listener once it’s no longer needed.

Also note, if your object is moving too fast,this might cause problems because Runtime events fire once every frame and the quickest it can fire is 16.66ms (assuming 60 FPS). In that case you might wanna try physics bodies and preCollision.

Btw, is your object just randomly moving around on the screen or it reacts to user input (like the user moving it to certain position?). If it is user input, you could check it once the user has made a move, instead of checking it every frame. 

How is the object being moved?  

If you’re using a transition, this is easy.

local circle = display.newCircle( 0, 0, 10 ) transition.to( circle, { x = 330, y = 250, onComplete = display.remove } )

If you’re going to use the suggestion by ‘farjadfarabi_czs’, I’d make a few changes:

local obj = display.newCircle(10,100,100) obj.targetX = 330 obj.targetY = 250 obj.maxDelta = 0.5 obj.enterFrame = function( self ) if( self.removeSelf == nil ) then Runtime:removeEventListner( "enterFrame", self ) return end local dx = math.abs(self.x - self.targetX) local dy = math.abs(self.y - self.targetY) if( dx \<= self.maxDelta and dy \<= self.maxDelta ) then Runtime:removeEventListner( "enterFrame", self ) display.remove(self) end end Runtime:addEventListner( "enterFrame", obj ) obj = nil

This is self-contained, safe, and allows for a certain amount of ‘wiggle’ room for matching your target <x,y> position. 

What roaminggamer said, if you’re moving the object to a specific spot.

Or, if the object is more “free range” and if/when it hits that specific spot you want it to vanish, here’s another option as long as you can add a physics body to your moving object…

Create an invisible object at the correct location (make sure the isSensor property is set to true on that) and then just watch for a collision between that and the moving object. When you get the collision, you know the object has arrived at that location.

If you need more info on physics bodies, see this: https://docs.coronalabs.com/guide/physics/physicsBodies/index.html

And here’s info on physics collisions: https://docs.coronalabs.com/guide/physics/collisionDetection/index.html

 Jay

[Edit: Sorry, didn’t see that farjadfarabi_czs had already recommended the physics approach!]

roaminggamer pretty much gave you an all around approach, just follow that as he showed. :slight_smile: I was trying to simply give a general idea. 

Thank you all so much for all the replies. I think I understand it now, and have it all working perfectly :). I really appreciate the help.

You’re welcome.  Please note ‘farjadfarabi_czs’ had a perfectly workable solution.  I simply worried you might run into precision issues as that example would only work for an exact match.  This might lead to confusion, so I amended it a bit.

Cheers,

Ed

You could try something like 

local object=display.newCircle(10,100,100) local myX=330;local myY=250; local function removeObject(event) if object.x==myX and object.y==myY then object:removeSelf();object=nil; Runtime:removeEventListener("enterFrame",removeObject) end end Runtime:addEventListener("enterFrame",removeObject)

Remember that runtime listeners keep running even after the function executes because they are global. So assuming you are done after a single execution of the function, you should remove the runtime listener once it’s no longer needed.

Also note, if your object is moving too fast,this might cause problems because Runtime events fire once every frame and the quickest it can fire is 16.66ms (assuming 60 FPS). In that case you might wanna try physics bodies and preCollision.

Btw, is your object just randomly moving around on the screen or it reacts to user input (like the user moving it to certain position?). If it is user input, you could check it once the user has made a move, instead of checking it every frame. 

How is the object being moved?  

If you’re using a transition, this is easy.

local circle = display.newCircle( 0, 0, 10 ) transition.to( circle, { x = 330, y = 250, onComplete = display.remove } )

If you’re going to use the suggestion by ‘farjadfarabi_czs’, I’d make a few changes:

local obj = display.newCircle(10,100,100) obj.targetX = 330 obj.targetY = 250 obj.maxDelta = 0.5 obj.enterFrame = function( self ) if( self.removeSelf == nil ) then Runtime:removeEventListner( "enterFrame", self ) return end local dx = math.abs(self.x - self.targetX) local dy = math.abs(self.y - self.targetY) if( dx \<= self.maxDelta and dy \<= self.maxDelta ) then Runtime:removeEventListner( "enterFrame", self ) display.remove(self) end end Runtime:addEventListner( "enterFrame", obj ) obj = nil

This is self-contained, safe, and allows for a certain amount of ‘wiggle’ room for matching your target <x,y> position. 

What roaminggamer said, if you’re moving the object to a specific spot.

Or, if the object is more “free range” and if/when it hits that specific spot you want it to vanish, here’s another option as long as you can add a physics body to your moving object…

Create an invisible object at the correct location (make sure the isSensor property is set to true on that) and then just watch for a collision between that and the moving object. When you get the collision, you know the object has arrived at that location.

If you need more info on physics bodies, see this: https://docs.coronalabs.com/guide/physics/physicsBodies/index.html

And here’s info on physics collisions: https://docs.coronalabs.com/guide/physics/collisionDetection/index.html

 Jay

[Edit: Sorry, didn’t see that farjadfarabi_czs had already recommended the physics approach!]

roaminggamer pretty much gave you an all around approach, just follow that as he showed. :slight_smile: I was trying to simply give a general idea. 

Thank you all so much for all the replies. I think I understand it now, and have it all working perfectly :). I really appreciate the help.

You’re welcome.  Please note ‘farjadfarabi_czs’ had a perfectly workable solution.  I simply worried you might run into precision issues as that example would only work for an exact match.  This might lead to confusion, so I amended it a bit.

Cheers,

Ed