inoperative removeSelf

removeSelf works in one spot, but in another it says it’s a nil value. Here’s my code, mostly mooched from the forums:

[blockcode]local physics = require(“physics”);
physics.start();
system.activate( “multitouch” )

physics.setDrawMode(“hybrid”)

display.setStatusBar( display.HiddenStatusBar )

_H = display.contentHeight
_W = display.contentWidth

print (_H … ", " … _W)

local worldLimits = { XMin=0 , YMin=0 , XMax=_W , YMax=_H }

local hand1 = display.newCircle (0, 0, 25)
hand1:setFillColor(255, 255, 255)
hand1.x = _W * 0.5
hand1.y = _H * 0.5

physics.addBody(hand1, {density=50, radius=20});
–Kill Joint

function destroy( event )
–if (hand1.weld ~= nil) then
–print (hand1.weld)
hand1.weld:removeSelf()
–end
end

function dragBody( event )
if (event.phase == “began”) then
if (hand1.weld ~= nil) then
timer.performWithDelay( 0, destroy, 1 )
end
– set drag focus to the ball to be dragged
display.getCurrentStage():setFocus( event.target )
event.target.touch = physics.newJoint( “touch”, event.target, event.x, event.y )
–get rid of the joint
elseif (event.phase == “moved”) then
– drag the ball
event.target.touch:setTarget( event.x, event.y )
else
– stop dragging the ball
display.getCurrentStage():setFocus( nil )
event.target.touch:removeSelf()
end

– tell system we’ve handled the touch
return true
end

—[[
–Make joint

–…because that can’t be done in the collision event handler
function create( event )
– only attach the joint to balls and weld is not already attached (use a table to add multiple joints)
if (hand1.weld == nil and hand1.other ~= nil) then
– add joint
hand1.weld = physics.newJoint( “weld”, hand1, hand1.other, hand1.x, hand1.y )
– we don’t need to keep track of the other object to be jointed
hand1.other = nil
end
end

–Collision

– when collisions happen start the timer to add the joint because it can’t be done here
function hand1:collision( event )
– only attach the joint to balls, not walls!
if (event.other.type == “wall”) then
– keep track of the other object to join to
hand1.other = event.other
– start the timer on a very short expiry
timer.performWithDelay( 0, create, 1 )
end
end

–]]
hand1:addEventListener( “touch”, dragBody )
hand1:addEventListener( “collision”, hand1 )

camera = display.newGroup()
local function makePlatform (name, x, y, width, height)
local name = display.newRect (0, 0, width, height)
name:setFillColor(255, 246, 0)
name.x = x
name.y = y
physics.addBody(name, “static”, {bounce = .4, friction=5})
camera:insert(name)
name.type = “wall”
end

makePlatform (wall1, 0, worldLimits.YMax * 0.5, 5, worldLimits.YMax) --left wall
makePlatform (wall2, worldLimits.XMax, worldLimits.YMax * 0.5, 5, worldLimits.YMax) --right wall
makePlatform (ground, worldLimits.XMax * 0.5, worldLimits.YMax, worldLimits.XMax, 5)–bottom
makePlatform (ceiling, worldLimits.XMax * 0.5, 0, worldLimits.XMax, 5) --top
[/blockcode]

The idea is, the ball is draggable and sticks to walls, but is supposed to unstick if you grab it again. It won’t unstick; the problem is the removeSelf in the “Kill Joint” function near the top.

What’s wrong? [import]uid: 82342 topic_id: 15780 reply_id: 315780[/import]