"attempt to call method 'addEventListener' (a nil value)"

Hi, I’m trying to make a pool-shot effect on my first app, and I’m having difficulties, the latest one that I can’t figure out is this.  The error is: main.lua:86: attempt to call method ‘addEventListener’ (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 balls

local Ball = display.newImage(“Ball.png”)

Ball.x = 200

Ball.y = 250

physics.addBody( Ball, “dynamic” )

local Ball2 = display.newImage(“Ball.png”)

Ball2.x = 200

Ball2.y = 50

physics.addBody( Ball2, “dynamic” )

local PlayerChar = display.newImage( “PlayerChar.png”)

PlayerChar.x = 50

PlayerChar.y = 160

physics.addBody( PlayerChar, “dynamic” )

PlayerChar.linearDamping = 0.3

PlayerChar.angularDamping = 0.8

PlayerChar.isBullet = true --force continue collision detection to stop really fast shots from breaking things

PlayerChar = { 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

        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]

If you remove that line, does the PlayerChar show up on the screen?

Yes.

Can you verify what line 86 is in your main.lua?

Line 86 is:

PlayerChar:addEventListener(  “touch”, shot )

I see it now.  This line:

PlayerChar = { density=0.8, friction=0.2, bounce=0.5, radius=15 }

Is changing the PlayerChar object to a table.  Perhaps you meant to name it PlayerCharBody and add it to the physics.addBody of PlayerChar?  

OOOOHHH, I’m dumb… Thanks.

However, now that the error isn’t coming up, it doesn’t respond to touch at all.

I added 2 lines (I’m using the SimplePool sample app as my reference) to the code, now if I click the Player Character it throws up an error saying:

main.lue:68: attempt to index field ‘parent’ (a nil value)

The 2 lines I entered were:

[lua]if ( myLine ) then

–line throwing up the error (line #61 above)

                myLine.parent:remove( myLine ) – erase previous line, if any

            end

–lines I added

            myLine = display.newLine( t.x,t.y, event.x,event.y )

            myLine:setColor( 255, 255, 255, 50 )

            myLine.width = 8[/lua]

If you remove that line, does the PlayerChar show up on the screen?

Yes.

Can you verify what line 86 is in your main.lua?

Line 86 is:

PlayerChar:addEventListener(  “touch”, shot )

I see it now.  This line:

PlayerChar = { density=0.8, friction=0.2, bounce=0.5, radius=15 }

Is changing the PlayerChar object to a table.  Perhaps you meant to name it PlayerCharBody and add it to the physics.addBody of PlayerChar?  

OOOOHHH, I’m dumb… Thanks.

However, now that the error isn’t coming up, it doesn’t respond to touch at all.

I added 2 lines (I’m using the SimplePool sample app as my reference) to the code, now if I click the Player Character it throws up an error saying:

main.lue:68: attempt to index field ‘parent’ (a nil value)

The 2 lines I entered were:

[lua]if ( myLine ) then

–line throwing up the error (line #61 above)

                myLine.parent:remove( myLine ) – erase previous line, if any

            end

–lines I added

            myLine = display.newLine( t.x,t.y, event.x,event.y )

            myLine:setColor( 255, 255, 255, 50 )

            myLine.width = 8[/lua]