Losing the event object through a timer inside a function

Hi guys,

I am trying to make the radial gravity work as in the tutorial (https://coronalabs.com/blog/2013/04/09/physics-radial-gravity-and-predicting-trajectory/)).

However I get an index nil value error when the ball hits the first circle. The error is happening because the event.other.x inside the onDelay function is nil (the event.other value doesn’t make it from the timer into the function). If I change the event.other.x to field.x then it works but obviously this is not ideal for more than one field object. Is there a way to pass this info from the collision event through the timer into the function?

Thanks,

Mark

local field = display.newCircle( 0, 0, 50 ) ; field.alpha = 0.3 field.name = "field" field.x = display.contentCenterX-40 ; field.y = display.contentCenterY-100 physics.addBody( field, "static", { isSensor=true, radius=80 } ) local field2 = display.newCircle( 0, 0, 50 ) ; field.alpha = 0.3 field2.name = "field2" field2.x = display.contentCenterX+60 ; field2.y = display.contentCenterY+100 physics.addBody( field2, "static", { isSensor=true, radius=80 } ) local ball = display.newCircle( 0, -50, 10 ) ; field.alpha = 0.3 ball.x = 50 ball.name = "ball" ball.hasJoint = false physics.addBody( ball, "dynamic", { isSensor=true, radius=10 } ) --COLLISION HANDLER function objectCollide( self, event ) -- print(self.name) --print(event.other.name) local otherName = event.other.name print(event.other) local function onDelay( event ) --local action = "" if ( action == "makeJoint" ) then self.hasJoint = true self.touchJoint = physics.newJoint( "touch", self, self.x, self.y ) self.touchJoint.frequency = 0.25 self.touchJoint.dampingRatio = 0.0 self.touchJoint:setTarget( event.other.x, event.other.y ) print(event.other) --print("timer executed") elseif ( action == "leftField" ) then self.hasJoint = false ; self.touchJoint:removeSelf() ; self.touchJoint = nil --print("left field") end end if ( event.phase == "began" and otherName == "field" and self.hasJoint == false ) then --print("timer triggered") local tr = timer.performWithDelay( 10, onDelay, 1 ) ; action = "makeJoint" elseif ( event.phase == "ended" and otherName == "field" and self.hasJoint == true ) then local tr = timer.performWithDelay( 10, onDelay ) ; action = "leftField" end end ball.collision = objectCollide ball:addEventListener( "collision", ball )

Try removing the ‘event’ parameter in: local function onDelay(event)
 

Dave

Wow thanks Davethat works perfectly, Trying to get my head round why though, is it possible to explain?

Try removing the ‘event’ parameter in: local function onDelay(event)
 

Dave

Wow thanks Davethat works perfectly, Trying to get my head round why though, is it possible to explain?