I have a function for radial gravity that creates a touch join between an object and another object which draws object 1 towards the center of object 2. This works perfectly, however the touch joint is not removed once object 1 has left the radius of object 2. I have a function to remove the touch joint but it doesn’t seem to be working, anyone know why?
local field = display.newCircle( scene.perRunGroup, 0, 0, 150) ; field.alpha = 0.3 field.name = "field" field.x = 600 field.y = 300 physics.addBody( field, "static", { isSensor=true, radius=100 } ) -------------- Create the touch joint between eggplant and the field function eggplant:collision( event ) local self = event.target local otherName = event.other.name local function onDelay() if (self.action == "makeJoint" ) then self.hasJoint = true self.touchJoint = physics.newJoint( "touch", self, self.x, self.y ) self.touchJoint.frequency = -1 self.touchJoint.dampingRatio = 0 self.touchJoint:setTarget( event.other.x, event.other.y ) self.action = nil end end if ( event.phase == "began" and otherName == "field" and self.hasJoint == nil ) then local tr = timer.performWithDelay( 10, onDelay ) eggplant.action = "makeJoint" end end eggplant:addEventListener( "collision", eggplant ) ---------------- Remove touch joint between eggplant and field function eggplantCollide( self, event ) local otherName = event.other.name local function onDelay( event ) local action = "" elseif ( action == "leftField" ) then self.hasJoint = false ; self.touchJoint:removeSelf() ; self.touchJoint = nil end end elseif ( event.phase == "ended" and otherName == "field" and self.hasJoint == true ) then local tr = timer.performWithDelay( 10, onDelay ) ; tr.action = "leftField" end end eggplant:addEventListener( "collision", eggplant )
It’s just the removing of the touch joint that doesn’t seem to be working