"attempt to index local 'event' (a nil value)"

Hi, I’m working on a magnet that pulls a character toward it, the only problem is, it throws up this error that I can’t figure out. “attempt to index local ‘event’ (a nil value)” and it’s killing me. Any help?

Here’s my code:

The line in question is 152.

[lua]


– main.lua


– Your code here

local physics = require(“physics”)

physics.start()

physics.setGravity(0, 0)

physics.setScale( 10 )

physics.setDrawMode( “normal” )

local magnetPull = 0.25

local n = 0

m = {}

m.random = math.random

–local state = display.newGroup()

local force_multiplier = ( 10 )

local velocity = m.random(50,100)

–Hiding status bar

display.setStatusBar( display.HiddenStatusBar )

– Setting background color

display.setDefault( “background”, 100,255,250 )

–code for gravity field

–Code for balls teehee

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

Ball.x = 200

Ball.y = 250

physics.addBody( Ball, “static” )

local function exitScene( event )

    if PlayerChar.y > display.contentHeight then

        timer.performWithDelay( 50, resetPlayerChar )

        print(“exitScene”)

    end

    if Ball.y < display.contentHeight then

        timer.performWithDelay( 50, resetPlayerChar )

        print(“exitScene”)

    end

    if Ball.x > display.contentWidth then

        timer.performWithDelay( 50, resetPlayerChar )

        print(“exitScene”)

    end

    if Ball.x  < display.contentWidth then

        timer.performWithDelay( 50, resetPlayerChar )

        print(“exitScene”)

    end

end

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

Ball2.x = 200

Ball2.y = 50

physics.addBody( Ball2, “static” )

–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 } )

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

PlayerChar.x = 50

PlayerChar.y = 160

physics.addBody( PlayerChar, 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=1, bounce=0, radius=15 }

target = display.newImage( “target.png” )

target.x = PlayerChar.x; target.y = PlayerChar.y; target.alpha = 0

–firing

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 )

            display.remove(myLine)

            print(“myLine removed”)

        end

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

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

            myLine.width = 8

            print(“myLine added”)

            

            

            

        elseif “ended” == phase or “cancelled” == phase then

            Runtime:removeEventListener( “enterFrame”, startRotation )

            if( myLine ) then

        --myLine.parent:remove( myLine )

        display.remove(myLine)

        print("–myLine removed")

        end

        

        --move the PlayerChar…

        t:applyForce( (t.x - event.x), (t.y - event.y), t.x, t.y )

        end

        

        

        

        local hideTarget = transition.to( target, { alpha=0, xScale=1.0, time=200, onComplete=stopRotation } )

        

        

    end

return true

end    

–magnet/gravity

function objectCollide( self, event )

local otherName = event.other.name

    

    local function onDelay( event )

    

        local action = “”

        if ( event.source ) then 

            action = event.source.action

            timer.cancel( event.source ) 

        end

        

        

        if ( action == “makeJoint” ) then

        

            self.hasJoint = true

            self.touchJoint = physics.newJoint( “touch”, field, field.x, field.y )

            self.touchJoint.frequency = 0.25

            self.touchJoint.dampingRatio = 0.0

            self.touchJoint:setTarget( Ball2.x, Ball2.y )

            print( “done” )

        end

    end

    if ( event.phase == “began” and otherName == “field” and self.hasJoint == false ) then

        local tr = timer.performWithDelay( 10, onDelay ) 

        tr.action = ( “makeJoint” )

        print( “joint made” )

    end

end

PlayerChar:addEventListener( “touch”, shot )

PlayerChar:addEventListener( “collision”, objectCollide )

local function resetPlayerChar()

    PlayerChar:setLinearVelocity( 0, 0 )

    PlayerChar.alpha = 0    

    PlayerChar.x = 50

    PlayerChar.y = 160

    local dropPlayerChar = transition.to( PlayerChar, { alpha=1.0, xScale=1.0, yScale = 1.0, time=1000 } )

    

end

function onCollision( event )

    print( “reset” )

    timer.performWithDelay( 50, resetPlayerChar )

end

field:addEventListener( “collision”, onCollision )

[/lua]

Where is line 153?

Hi @zimofrite,

Line 152 points to the fact that the “other” object involved in the collision doesn’t have the “name” property on it. Make sure you’ve established that property before the collision occurs. Also, remember that all collisions have both a “began” and “ended” phase to them, so typically you should filter out which one you want to check for (“began” is the most common).

Best regards,

Brent

I’m guessing the ‘self’ as first param on the method is not quite right.

function objectCollide( self, event )

Have you tried without the param?

function objectCollide( event )

You have your collision setup as a function based collision, not a table/object based collision.  Simply remove the self parameter and from the objectCollide() function and it should solve your problem.

Where is line 153?

Hi @zimofrite,

Line 152 points to the fact that the “other” object involved in the collision doesn’t have the “name” property on it. Make sure you’ve established that property before the collision occurs. Also, remember that all collisions have both a “began” and “ended” phase to them, so typically you should filter out which one you want to check for (“began” is the most common).

Best regards,

Brent

I’m guessing the ‘self’ as first param on the method is not quite right.

function objectCollide( self, event )

Have you tried without the param?

function objectCollide( event )

You have your collision setup as a function based collision, not a table/object based collision.  Simply remove the self parameter and from the objectCollide() function and it should solve your problem.