How to destroy an object if there to far away from an object?

Hey I need help on something this may sound complicated I will try to best explain. Ok so basically what I’m trying to do is have an object be destroyed if there to far away from something. Like example the ball is chasing a block. The ball is 200 feet away from the block. What I’m trying to do is if the block ball becomes further away from the block the game is over.

This is how I tried to do with the code

local function destroy() if (ball.x \> object.x) then display.removeSelf(ball) end end Runtime:addEventListener( "enterFrame", destroy )

I know is wrong. Can anyone please help me help is really appreciated

:slight_smile: [import]uid: 17058 topic_id: 21414 reply_id: 321414[/import]

Sounds like you need to check the difference between the two positions.

[code]
local distLimit = 200

local function checkFrame(event)
if (object.x - ball.x) > distLimit then
–the two objects are now too far apart
–destroy
display.remove(ball)
end
end

Runtime:addEventListener(“enterFrame”,checkFrame)
[/code] [import]uid: 94868 topic_id: 21414 reply_id: 84792[/import]

it thats all the code that needs for the ball to destroyed [import]uid: 17058 topic_id: 21414 reply_id: 84794[/import]

Edited the above code to be a little more fool-proof, assuming the object can be 200 units away in any direction.

[code]
local distLimit = 200

local function checkFrame(event)
if (math.abs(object.x - ball.x)) > distLimit then
–the two objects are now too far apart
–destroy
display.remove(ball)
end
end

Runtime:addEventListener(“enterFrame”,checkFrame)
[/code] [import]uid: 72820 topic_id: 21414 reply_id: 84805[/import]

@lethal_90 does this work for destroying the ball if the object is more than 200 units away?

EDIT: Thanks a lot :slight_smile:
[import]uid: 17058 topic_id: 21414 reply_id: 84809[/import]