Hi, I’m trying to make a slingshot for a game I’m making, but I’m getting this error when I try to drag it: "main.lua:75: attempt to index field ‘parent’ (a nil value)
My code:
[lua]local physics = require(“physics”)
physics.start()
physics.setGravity(0, 0)
physics.setScale( 60 )
local n = 0
–Hiding status bar
display.setStatusBar( display.HiddenStatusBar )
– Setting background color
display.setDefault( “background”, 100,255,250 )
–code for gravity field
–local field = display.newCircle( 0, 0, 100 ) ; field.alpha = 0.3
–field.name = “field”
–field.x = Ball2.x ; field.y = Ball2.y
–physics.addBody( field, “static”, { isSensor = true, radius = 100 } )s
–Code for balls
local Ball = display.newImage(“Ball.png”)
Ball.x = 200
Ball.y = 250
physics.addBody( Ball, “static” )
local Ball2 = display.newImage(“Ball.png”)
Ball2.x = 200
Ball2.y = 50
physics.addBody( Ball2, “static” )
local PlayerChar = display.newImage( “PlayerChar.png”)
PlayerChar.x = 50
PlayerChar.y = 160
physics.addBody( PlayerChar, “dynamic”, PlayerCharBody )
PlayerChar.linearDamping = 0.3
PlayerChar.angularDamping = 0.8
PlayerChar.isBullet = true --force continue collision detection to stop really fast shots from breaking things
PlayerCharBody = { density=0.8, friction=0.2, bounce=0.5, radius=15 }
target = display.newImage( “target.png” )
target.x = PlayerChar.x; target.y = PlayerChar.y; target.alpha = 0
local function shot( event )
local t = event.target
local phase = event.phase
if “began” == phase then
display.getCurrentStage():setFocus( t )
t.isFocus = true
--stop current ball motion, if any
t:setLinearVelocity( 0, 0 )
t.angularVelocity = 0
target.x = t.x
target.y = t.y
startRotation = function()
target.rotation = target.rotation + 4
end
Runtime:addEventListener( “enterFrame”, startRotation )
local showTarget = transition.to( target, { alpha = 0.4, xScale=0.4, yScale=0.4, time=200 } )
myLine = nil
elseif t.isFocus then
if “moved” == phase then
if( myLine ) then
myLine.parent:remove( myLine ) --erase previous line, if any
end
myLine = display.newLine( t.x, t.y, event.x, event.y )
myLine:setColor( 255, 255, 255, 50 )
myLine.width = 8
elseif “ended” == phase or “cancelled” == phase then
Runtime:removeEventListener( “enterFrame”, startRotation )
end
local hideTarget = transition.to( target, { alpha=0, xScale=1.0, time=200, onComplete=stopRotation } )
if( myLine ) then
myLine.parent:remove( myLine )
end
end
return true
end
PlayerChar:addEventListener( “touch”, shot )[/lua]
The line that’s throwing up the error is #68 on this.
). Perhaps you intended myLine to be removed in the ended phase around line 74 (instead of at line 80)? If you move that removal and put it inside the ended phase, does that behave as you expected?