How to destroy object when it hits edge of screen

Hi 

I need some help figuring out how I can destroy an object when it hits the edge of the screen. Right now I have an object that runs in a random direction and I want it to destroy itself (possibly put in a explosion effect which I can worry about later) and then the object reappears in the center and runs in another direction. 

Note- Center is basically take your iPhone and the center point of the screen. Edges are all four sides anywhere it can possibly touch on all four sides. 

Any help is appreciated 

Thank you

You can do something like (I have not tested):

local physics = require("physics") physics.start() local SCREEN\_BOTTOM = display.viewableContentHeight - display.screenOriginY local SCREEN\_TOP = display.screenOriginY local SCREEN\_RIGHT = display.viewableContentWidth - display.screenOriginX local SCREEN\_LEFT = display.screenOriginX local SCREEN\_CENTER\_X = display.contentWidth \* 0.5 local WIDTH = display.actualContentWidth local HEIGHT = display.actualContentHeight local WALL\_SIZE = 50 local function createWall(params) local wall = display.newRect( params.group, params.x, params.y, params.w, params.h ) wall.isVisible = false physics.addBody(wall, "static") return wall end local function onWallCollision(self, event) if event.phase == "ended" then if event.other.type == "my\_object" then -- Here you can destroy your object and create another display.remove(event.other) end end end local function createWalls() local group = display.newGroup() local right = createWall({ group=group, w=WALL\_SIZE, h=HEIGHT, x = SCREEN\_RIGHT + WALL\_SIZE\*0.5, y = SCREEN\_TOP + HEIGHT\*0.5 }) right.collision = onWallCollision right:addEventListener("collision", right) local left = createWall({ group=group, w=WALL\_SIZE, h=HEIGHT, x=SCREEN\_LEFT - WALL\_SIZE\*0.5, y=SCREEN\_TOP + HEIGHT\*0.5 }) left.collision = onWallCollision left:addEventListener("collision", left) local top = createWall({ group=group, w=WIDTH, h=WALL\_SIZE, x=SCREEN\_CENTER\_X, y=SCREEN\_TOP - WALL\_SIZE\*0.5 }) top.collision = onWallCollision top:addEventListener("collision", top) local bottom = createWall({ group=group, w=WIDTH, h=WALL\_SIZE, x=SCREEN\_CENTER\_X, y=SCREEN\_BOTTOM + WALL\_SIZE\*0.5 }) bottom.collision = onWallCollision bottom:addEventListener("collision", bottom) end createWalls()

A fairly simple way is to create a rectangular object the size of your screen. While your ‘runner’ object is colliding with the ‘screen’ object you know it is within the display. Once the two objects are no longer colliding, the ‘runner’ object has left the display area and can be destroyed.

You can tweak the size of the ‘screen’ object to control how close to the screen edge the ‘runner’ has to be before it gets destroyed.

This has the added bonus that you cannot miss a collision with a wall because the ‘runner’ has somehow jumped it by travelling so fast that it never technically collided with it. 

Jules

You could do this fairly crudely without physics by just comparing the x/y values against some given boundaries:

 

local minX = display.screenOriginX local minY = display.screenOriginY local maxX = display.viewableContentWidth + -1\* display.screenOriginX local maxY = display.viewableContentHeight + -1\* display.screenOriginY local objectSize = 30 local myObj local function newObj() myObj = display.newImageRect("FireTV\_menu.png", objectSize, objectSize) myObj.x, myObj.y = display.contentWidth \* 0.5, display.contentHeight \* 0.5 myObj.xVel, myObj.yVel = -2 + math.random(0, 4), -2 + math.random(0, 4) if math.abs(myObj.xVel) \< 0.25 then myObj.xVel = myObj.xVel \* 2 end if math.abs(myObj.yVel) \< 0.25 then myObj.yVel = myObj.yVel \* 2 end end local function update() myObj.x = myObj.x + myObj.xVel myObj.y = myObj.y + myObj.yVel if myObj.x \< minX-objectSize or myObj.x \> maxX + objectSize or myObj.y \< minY-objectSize or myObj.y \> maxY + objectSize then print("delete obj") myObj:removeSelf() newObj() end end newObj() Runtime:addEventListener("enterFrame", update)

You might also try the module I posted here (it has both pessimistic and optimistic detection):

https://forums.coronalabs.com/topic/55035-how-to-determine-if-sprite-is-on-screen/

You can do something like (I have not tested):

local physics = require("physics") physics.start() local SCREEN\_BOTTOM = display.viewableContentHeight - display.screenOriginY local SCREEN\_TOP = display.screenOriginY local SCREEN\_RIGHT = display.viewableContentWidth - display.screenOriginX local SCREEN\_LEFT = display.screenOriginX local SCREEN\_CENTER\_X = display.contentWidth \* 0.5 local WIDTH = display.actualContentWidth local HEIGHT = display.actualContentHeight local WALL\_SIZE = 50 local function createWall(params) local wall = display.newRect( params.group, params.x, params.y, params.w, params.h ) wall.isVisible = false physics.addBody(wall, "static") return wall end local function onWallCollision(self, event) if event.phase == "ended" then if event.other.type == "my\_object" then -- Here you can destroy your object and create another display.remove(event.other) end end end local function createWalls() local group = display.newGroup() local right = createWall({ group=group, w=WALL\_SIZE, h=HEIGHT, x = SCREEN\_RIGHT + WALL\_SIZE\*0.5, y = SCREEN\_TOP + HEIGHT\*0.5 }) right.collision = onWallCollision right:addEventListener("collision", right) local left = createWall({ group=group, w=WALL\_SIZE, h=HEIGHT, x=SCREEN\_LEFT - WALL\_SIZE\*0.5, y=SCREEN\_TOP + HEIGHT\*0.5 }) left.collision = onWallCollision left:addEventListener("collision", left) local top = createWall({ group=group, w=WIDTH, h=WALL\_SIZE, x=SCREEN\_CENTER\_X, y=SCREEN\_TOP - WALL\_SIZE\*0.5 }) top.collision = onWallCollision top:addEventListener("collision", top) local bottom = createWall({ group=group, w=WIDTH, h=WALL\_SIZE, x=SCREEN\_CENTER\_X, y=SCREEN\_BOTTOM + WALL\_SIZE\*0.5 }) bottom.collision = onWallCollision bottom:addEventListener("collision", bottom) end createWalls()

A fairly simple way is to create a rectangular object the size of your screen. While your ‘runner’ object is colliding with the ‘screen’ object you know it is within the display. Once the two objects are no longer colliding, the ‘runner’ object has left the display area and can be destroyed.

You can tweak the size of the ‘screen’ object to control how close to the screen edge the ‘runner’ has to be before it gets destroyed.

This has the added bonus that you cannot miss a collision with a wall because the ‘runner’ has somehow jumped it by travelling so fast that it never technically collided with it. 

Jules

You could do this fairly crudely without physics by just comparing the x/y values against some given boundaries:

 

local minX = display.screenOriginX local minY = display.screenOriginY local maxX = display.viewableContentWidth + -1\* display.screenOriginX local maxY = display.viewableContentHeight + -1\* display.screenOriginY local objectSize = 30 local myObj local function newObj() myObj = display.newImageRect("FireTV\_menu.png", objectSize, objectSize) myObj.x, myObj.y = display.contentWidth \* 0.5, display.contentHeight \* 0.5 myObj.xVel, myObj.yVel = -2 + math.random(0, 4), -2 + math.random(0, 4) if math.abs(myObj.xVel) \< 0.25 then myObj.xVel = myObj.xVel \* 2 end if math.abs(myObj.yVel) \< 0.25 then myObj.yVel = myObj.yVel \* 2 end end local function update() myObj.x = myObj.x + myObj.xVel myObj.y = myObj.y + myObj.yVel if myObj.x \< minX-objectSize or myObj.x \> maxX + objectSize or myObj.y \< minY-objectSize or myObj.y \> maxY + objectSize then print("delete obj") myObj:removeSelf() newObj() end end newObj() Runtime:addEventListener("enterFrame", update)

You might also try the module I posted here (it has both pessimistic and optimistic detection):

https://forums.coronalabs.com/topic/55035-how-to-determine-if-sprite-is-on-screen/