Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

"attempt to index local 'event' (a nil value)"
Started by Brdybrbnk Sep 27 2013 01:06 PM

- - - - -
4 replies to this topic
[TOPIC CONTROLS]
This topic has been archived. This means that you cannot reply to this topic.
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

Brdybrbnk

[GLOBAL: userInfoPane.html]
Brdybrbnk
  • Enthusiast

  • 65 posts
  • Corona SDK

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.

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


[TOPIC: post.html]
#2

piotrz55

[GLOBAL: userInfoPane.html]
piotrz55
  • Contributor

  • 754 posts
  • Corona SDK

Where is line 153?

[TOPIC: post.html]
#3

bjsorrentino

[GLOBAL: userInfoPane.html]
bjsorrentino
  • Veteran

  • 8,506 posts
  • Corona SDK

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



[TOPIC: post.html]
#4

rodrigo.chapuis

[GLOBAL: userInfoPane.html]
rodrigo.chapuis
  • Observer

  • 22 posts
  • Corona SDK

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 )



[TOPIC: post.html]
#5

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 26,393 posts
  • Enterprise

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.




[topic_controls]
 
[/topic_controls]