Magnet not working?

Hi, I’m trying to make a magnet, but it’s just not working. I’m using (http://www.coronalabs.com/blog/2013/04/09/physics-radial-gravity-and-predicting-trajectory/)]this site as my guide. But for some reason, it’s just not acting on my object. What did I do wrong? That part that should be affecting gravity starts at line 146.

This is my code:

[lua]

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

–boundries

–left side

local boundryLeft = display.newRect(-42, 0, 5, 480)

boundryLeft.strokeWidth = 5

boundryLeft:setFillColor( 0, 0, 0 )

boundryLeft:setStrokeColor( 0, 0, 0 )

physics.addBody( boundryLeft, “static” )

–top

local boundryTop = display.newRect( -42, 0, 640, 5 )

boundryTop.strokeWidth = 5

boundryTop:setFillColor( 0, 0, 0 )

boundryTop:setStrokeColor( 0, 0, 0 )

physics.addBody( boundryTop, “static” )

–right side

local boundryRight = display.newRect( 520, 0, 5, 480 )

boundryRight.strokeWidth = 5

boundryRight:setFillColor( 0, 0, 0 )

boundryRight:setStrokeColor( 0, 0, 0 )

physics.addBody( boundryRight, “static” )

–bottom

local boundryBottom = display.newRect( -42, 315, 640, 5 )

boundryBottom.strokeWidth = 5

boundryBottom:setFillColor( 0, 0, 0 )

boundryBottom:setStrokeColor( 0, 0, 0 )

physics.addBody( boundryBottom, “static” )

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

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

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

PlayerChar:addEventListener( “collision”, onCollision )

[/lua]

You’re not calling the function objectCollide anywhere in your code.  That’s why the code isn’t executing…

Wow… I can’t believe I missed that… Naturally of course now there’s an error (I’m really sorry I’m very new to this whole thing and learning as I go), “148: attempt to index local ‘event’ (a nil value)”

I think I might kind of understand why it’s there, but going through the code of the program I’m referencing there’s no other mention of event.other.name (what’s giving me the trouble) elsewhere in the document.

5 days later, it’s still not working.

Can you post your updated code?   You probably need to move your objectCollide function to the top

This is the updated code:

[lua]

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

–boundries

–left side

local boundryLeft = display.newRect(-42, 0, 5, 480)

boundryLeft.strokeWidth = 5

boundryLeft:setFillColor( 0, 0, 0 )

boundryLeft:setStrokeColor( 0, 0, 0 )

physics.addBody( boundryLeft, “static” )

–top

local boundryTop = display.newRect( -42, 0, 640, 5 )

boundryTop.strokeWidth = 5

boundryTop:setFillColor( 0, 0, 0 )

boundryTop:setStrokeColor( 0, 0, 0 )

physics.addBody( boundryTop, “static” )

–right side

local boundryRight = display.newRect( 520, 0, 5, 480 )

boundryRight.strokeWidth = 5

boundryRight:setFillColor( 0, 0, 0 )

boundryRight:setStrokeColor( 0, 0, 0 )

physics.addBody( boundryRight, “static” )

–bottom

local boundryBottom = display.newRect( -42, 315, 640, 5 )

boundryBottom.strokeWidth = 5

boundryBottom:setFillColor( 0, 0, 0 )

boundryBottom:setStrokeColor( 0, 0, 0 )

physics.addBody( boundryBottom, “static” )

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

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

forumPlayerChar: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

–PlayerChar:addEventListener( “collision”, onCollision )

[/lua]

You’re not calling the function objectCollide anywhere in your code.  That’s why the code isn’t executing…

Wow… I can’t believe I missed that… Naturally of course now there’s an error (I’m really sorry I’m very new to this whole thing and learning as I go), “148: attempt to index local ‘event’ (a nil value)”

I think I might kind of understand why it’s there, but going through the code of the program I’m referencing there’s no other mention of event.other.name (what’s giving me the trouble) elsewhere in the document.

5 days later, it’s still not working.

Can you post your updated code?   You probably need to move your objectCollide function to the top

This is the updated code:

[lua]

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

–boundries

–left side

local boundryLeft = display.newRect(-42, 0, 5, 480)

boundryLeft.strokeWidth = 5

boundryLeft:setFillColor( 0, 0, 0 )

boundryLeft:setStrokeColor( 0, 0, 0 )

physics.addBody( boundryLeft, “static” )

–top

local boundryTop = display.newRect( -42, 0, 640, 5 )

boundryTop.strokeWidth = 5

boundryTop:setFillColor( 0, 0, 0 )

boundryTop:setStrokeColor( 0, 0, 0 )

physics.addBody( boundryTop, “static” )

–right side

local boundryRight = display.newRect( 520, 0, 5, 480 )

boundryRight.strokeWidth = 5

boundryRight:setFillColor( 0, 0, 0 )

boundryRight:setStrokeColor( 0, 0, 0 )

physics.addBody( boundryRight, “static” )

–bottom

local boundryBottom = display.newRect( -42, 315, 640, 5 )

boundryBottom.strokeWidth = 5

boundryBottom:setFillColor( 0, 0, 0 )

boundryBottom:setStrokeColor( 0, 0, 0 )

physics.addBody( boundryBottom, “static” )

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

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

forumPlayerChar: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

–PlayerChar:addEventListener( “collision”, onCollision )

[/lua]