Attempt to call method 'applyForce'

Hi!

I want to create new obiect but output throw me

attempt to call method 'applyForce' (a nil value)

my function to create ‘ball’

function startBall()     local ball = display.newRect(\_W + math.random( -400,400),\_H + math.random( -300,300) ,30, 40);     physics.addBody(ball, "dynamic");     ball.myName = "ball";     ball:applyForce(math.random( -20,20), math.random( -20, 20), ball.x, ball.y ); end

And my onColision where i invoke my function

function onCollision( event )         if ( event.phase =="began" ) then         local pacman = event.object1;         local ball = event.object2; if(ball.myName == "ball") then         ball:removeSelf();         startBall()     end end end

When i invoke my method outside onCollision everythink work fine.

Thanks in advice!

First make sure that event.object2 is your ball and not pacman.

Does pacman has physics applied to it too?

If event.object2.myName == “ball” then it is ok, but if event.object2.myName is nil or some other value then object2 might be your pacman. 

i have solution

if(ball.myName == "ball" and pacman.myName == "eate") then                     ball:removeSelf();                     local dr = timer.performWithDelay( 50, startBall )                     dr.ball = square                 end  

First make sure that event.object2 is your ball and not pacman.

Does pacman has physics applied to it too?

If event.object2.myName == “ball” then it is ok, but if event.object2.myName is nil or some other value then object2 might be your pacman. 

i have solution

if(ball.myName == "ball" and pacman.myName == "eate") then                     ball:removeSelf();                     local dr = timer.performWithDelay( 50, startBall )                     dr.ball = square                 end