touchJoint:removeSelf() not working to break Joint

My object attaches with a joint perfectly fine.

When I exit the area to breakjoint, it does display the break according to the printed message. It just doesn’t break the joint with otherobject.touchJoint:removeSelf()

This is based off the Radial Gravity project.

I’m just struggling with the Joint Break Point, it just isn’t breaking and keeps a hold of the object.

      local function BlackHoleCollision(self,event)         local otherobject = event.other                           local function joint(event)         local action = ""                  if ( event.source ) then action = event.source.action ; timer.cancel( event.source ) end                  if action == "makejoint" then         print (action)         self.isSensor = true             otherobject.hasJoint = true             otherobject.touchJoint = physics.newJoint( "touch", otherobject,otherobject.x, otherobject.y)             otherobject.touchJoint.frequency = magnetPull             otherobject.touchJoint.dampingRatio = 0.3             otherobject.touchJoint:setTarget(  self.x, self.y )             else if action == "breakjoint" then             print (action)         otherobject.hasJoint = false         otherobject.touchJoint:removeSelf()         otherobject.touchJoint = nil         end         end         end              if ( event.phase == "began" ) then         print("started")                     local tr = timer.performWithDelay(50, joint)         tr.action = "makejoint"         else if ( event.phase == "ended" ) then         print("ended")         local tr = timer.performWithDelay(50, joint)         tr.action = "breakjoint" end end         end field.collision = BlackHoleCollision     field:addEventListener( "collision", field ) end

This is the error:

2013-06-06 17:13:33.708 Corona Simulator[383:707] Runtime error ...dom/Desktop/Build 6/Class Objects/BlackholeClass.lua:58: attempt to call method 'removeSelf' (a nil value) stack traceback: &nbsp;&nbsp; &nbsp;[C]: in function 'removeSelf' &nbsp;&nbsp; &nbsp;...dom/Desktop/Build 6/Class Objects/BlackholeClass.lua:58: in function '\_listener' &nbsp;&nbsp; &nbsp;?: in function \<?:141\> &nbsp;&nbsp; &nbsp;?: in function \<?:218\>

Hi @Dom Studios,

Unless this got posted into the forum incorrectly, are you intentionally using the nested…

[lua]

else if

[/lua]

Instead of the Lua…

[lua]

elseif

[/lua]

statements? This seems odd to me, and it might be messing up your logic handling.

Regards,

Brent

thanks for the reply.

no I was using ‘else if’ but have now tried ‘elseif’

and I am still getting the same result

any other suggestions?

Hello again,

Can you please post your revised code, with the “elseif” conditions in place?

Thanks,

Brent

Hi,

Here is what my code looks like now.

local function BlackHoleCollision(self,event) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local otherobject = event.other &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local function joint(event) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local action = "" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if ( event.source ) then action = event.source.action ; timer.cancel( event.source ) end &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if action == "makejoint" then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print (action) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;self.isSensor = true &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;otherobject.hasJoint = true &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;otherobject.touchJoint = physics.newJoint( "touch", otherobject,otherobject.x, otherobject.y) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;otherobject.touchJoint.frequency = magnetPull &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;otherobject.touchJoint.dampingRatio = 0.3 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;otherobject.touchJoint:setTarget(&nbsp; self.x, self.y ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif action == "breakjoint" then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print (action) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;otherobject.hasJoint = false &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;otherobject.touchJoint:removeSelf() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;otherobject.touchJoint = nil &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if ( event.phase == "began" ) then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("started")&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local tr = timer.performWithDelay(50, joint) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;tr.action = "makejoint" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif ( event.phase == "ended" ) then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("ended") &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local tr = timer.performWithDelay(50, joint) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;tr.action = "breakjoint" end end&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; field.collision = BlackHoleCollision&nbsp;&nbsp; &nbsp; field:addEventListener( "collision", field ) end

I have found the problem.

if otherobject.hasJoint == false and action == "makejoint" then

and

elseif otherobject.hasJoint == true and action == "breakjoint" then

Thank you Brent for replying in trying to help

Hi @Dom Studios,

Unless this got posted into the forum incorrectly, are you intentionally using the nested…

[lua]

else if

[/lua]

Instead of the Lua…

[lua]

elseif

[/lua]

statements? This seems odd to me, and it might be messing up your logic handling.

Regards,

Brent

thanks for the reply.

no I was using ‘else if’ but have now tried ‘elseif’

and I am still getting the same result

any other suggestions?

Hello again,

Can you please post your revised code, with the “elseif” conditions in place?

Thanks,

Brent

Hi,

Here is what my code looks like now.

local function BlackHoleCollision(self,event) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local otherobject = event.other &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local function joint(event) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local action = "" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if ( event.source ) then action = event.source.action ; timer.cancel( event.source ) end &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;if action == "makejoint" then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print (action) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;self.isSensor = true &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;otherobject.hasJoint = true &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;otherobject.touchJoint = physics.newJoint( "touch", otherobject,otherobject.x, otherobject.y) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;otherobject.touchJoint.frequency = magnetPull &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;otherobject.touchJoint.dampingRatio = 0.3 &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;otherobject.touchJoint:setTarget(&nbsp; self.x, self.y ) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif action == "breakjoint" then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print (action) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;otherobject.hasJoint = false &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;otherobject.touchJoint:removeSelf() &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;otherobject.touchJoint = nil &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;end &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if ( event.phase == "began" ) then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("started")&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local tr = timer.performWithDelay(50, joint) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;tr.action = "makejoint" &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;elseif ( event.phase == "ended" ) then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;print("ended") &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;local tr = timer.performWithDelay(50, joint) &nbsp;&nbsp; &nbsp;&nbsp;&nbsp; &nbsp;tr.action = "breakjoint" end end&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp; field.collision = BlackHoleCollision&nbsp;&nbsp; &nbsp; field:addEventListener( "collision", field ) end

I have found the problem.

if otherobject.hasJoint == false and action == "makejoint" then

and

elseif otherobject.hasJoint == true and action == "breakjoint" then

Thank you Brent for replying in trying to help