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.