Detecting Object Offscreen

local math, physics = require( “math” ), require( ‘physics’ )

physics.start()

physics.setGravity( 0, 0 )

display.setDefault( “background”, 0.9, 0.9, 0.9 )

local function rect1Collision()

        rect1:removeSelf()

        rect1Detect = false

end

local function levelCreate()

       rect1 = display.newRect( 360, 300, 1280, 15 )

       physics.addBody( rect1, “static”, { bounce = 1, friction = 0 } )

       rect1:addEventListener( “collision”, rect1Collision )

       rect1Detect = true

end

local function levelDestroy()

       if rect1Detect == false then

               levelCreate()

       end

end

local function circleLaunch( event )

       if event.phase == “began” and canLaunch == true then

              player = display.newCircle( event.x, event.y , 20 )

              physics.addBody( player, “dynamic”, { bounce = 1, friction = 0 } )

              line = display.newLine( player.x, player.y, event.x, event.y )

              line.strokeWidth = 5

       elseif event.phase == “moved” and canLaunch == true then

              line:removeSelf()

              line = display.newLine( player.x, player.y, event.x, event.y )

              line.strokeWidth = 5

       elseif ( event.phase == “ended” ) and canLaunch == true then

              speed = 600

              deltaX = event.x - player.x

              deltaY = event.y - player.y

              pythagDeltaX = deltaX / math.sqrt( math.pow( deltaX,2 ) + math.pow( deltaY,2 ) ) * -1

              pythagDeltaY = deltaY / math.sqrt( math.pow( deltaX,2 ) + math.pow( deltaY,2 ) ) * -1

              player:setLinearVelocity( pythagDeltaX  * speed, pythagDeltaY  * speed )

              canLaunch = false

              line:removeSelf()

       end

       return true

end

canLaunch = true

levelCreate()

rect1:addEventListener( “collision”, rect1Collision )

Runtime:addEventListener( “touch”, circleLaunch )

So basically, I’m creating a circle object inside a function, at the touch location. The problem I’m having is I want to detect when it goes off screen, and delete it when it does. I’ve tried using a timer on a function, and having an if statement inside that function which detects whether player.x > 1280 etc, so that it runs the statement over and over. However after it deletes it, it will say (player is a nil value) or something along those lines. I really have no clue what else to do. Thanks in advance.

This isn’t tested so i don’t know if this will work or not but here. (Ill be able to help further more when i get home tonight (PST) )

local centerX = display.contentCenterX local centerY = display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local object = display.newRect( centerX, centerY, 20, 20 ) transition.to( object, { time = 2000, y = centerY + actualH/2 + 100 }) local function removeObject() local leftSide = centerX - actualX/2 -- (- object.width/2) local rightSide = centerX + actualX/2 -- (+ object.width/2) local top = centerY - actualH/2 -- (- object.height/2) local bottom = centerY + actualH/2 -- (+ object.height/2) if object.x \< leftSide and object.x \> rightSide then display.remove(object) end if object.y \< top and object.y \> bottom then display.remove(object) end end Runtime:addEventListener( "enterFrame", removeObject )

–SonicX278

I use this all the time to stop animations of offscreen objects…

 local function offScreen(object) local bounds = object.contentBounds local sox, soy = display.screenOriginX, display.screenOriginY if bounds.xMax \< sox then return true end if bounds.yMax \< soy then return true end if bounds.xMin \> display.actualContentWidth - sox then return true end if bounds.yMin \> display.actualContentHeight - soy then return true end return false end

This isn’t tested so i don’t know if this will work or not but here. (Ill be able to help further more when i get home tonight (PST) )

local centerX = display.contentCenterX local centerY = display.contentCenterY local actualW = display.actualContentWidth local actualH = display.actualContentHeight local object = display.newRect( centerX, centerY, 20, 20 ) transition.to( object, { time = 2000, y = centerY + actualH/2 + 100 }) local function removeObject() local leftSide = centerX - actualX/2 -- (- object.width/2) local rightSide = centerX + actualX/2 -- (+ object.width/2) local top = centerY - actualH/2 -- (- object.height/2) local bottom = centerY + actualH/2 -- (+ object.height/2) if object.x \< leftSide and object.x \> rightSide then display.remove(object) end if object.y \< top and object.y \> bottom then display.remove(object) end end Runtime:addEventListener( "enterFrame", removeObject )

–SonicX278

I use this all the time to stop animations of offscreen objects…

 local function offScreen(object) local bounds = object.contentBounds local sox, soy = display.screenOriginX, display.screenOriginY if bounds.xMax \< sox then return true end if bounds.yMax \< soy then return true end if bounds.xMin \> display.actualContentWidth - sox then return true end if bounds.yMin \> display.actualContentHeight - soy then return true end return false end