destroying joints

Hi

I’m facing a problem with removing joints.

When clicking on the background, a ball collides with a rectangle shape. On collision a distance joint is created. Then when the ball is tapped the joint should be destroyed, but when I use myJoint:removeSelf() I get an error. I tried almost everything but I can’t figure it out.

I also was trying the same with a weld joint, but i get even more errors when i try to destroy it.

would greatly appreciate any help with correcting the following code

thanks

[code]
– remove status bar
display.setStatusBar( display.HiddenStatusBar )

– start physics
local physics = require( “physics” )
physics.start()
physics.setDrawMode( “hybrid” )
local ball
local other
local myJoint

local bg = display.newRect( 0, 0, display.contentWidth, display.contentHeight )
bg:setFillColor( 0, 0, 0 )

– creates a wall to bounce things off
local function addWall( x, y, width, height )
local wall = display.newRect( 0, 0, width, height )
wall.myName = “wall”
wall:setFillColor( 255, 255, 255 )
physics.addBody( wall, “static”, { friction=1, bounce=.5, density=1 } )
wall.x, wall.y = x, y
end

– create the walls round the edge of the screen
addWall( display.contentWidth/2, 0, display.contentWidth, 10 )
addWall( display.contentWidth, display.contentHeight/2, 10, display.contentHeight )
addWall( display.contentWidth/2, display.contentHeight, display.contentWidth, 10 )
addWall( 0, display.contentHeight/2, 10, display.contentHeight )

local rect = display.newRect(display.contentWidth * 0.4, (display.contentHeight * 0.5) - 30, 64, 30 )
physics.addBody( rect, “static”, { friction=0.5 } )
rect:setFillColor( 188, 143, 4 )
rect.myName = “rect”

ball = display.newCircle( display.contentWidth * 0.5, (display.contentHeight * 0.9) - 30, 20 )
ball.myName = “ball”
ball:setFillColor( math.random(150, 255), math.random(150, 255), math.random(150, 255) )
physics.addBody( ball, “dynamic”, { friction=1, bounce=.7, density=1, radius=radius } )

local function weld()
myJoint = physics.newJoint( “distance”, ball, other, ball.x, ball.y, other.x, other.y )
end

local function collide (event)
other = event.other
ball:removeEventListener(“collision”, collide)
timer.performWithDelay( 0, weld, 1 )
end

local function bounce (event)
ball:setLinearVelocity( 0, -450)
ball:addEventListener(“collision”, collide)
end

local function removeJoint (event)
myJoint:removeSelf()
end

local function removeJ (event)
local phase = event.phase

if “began” == phase then
removeJoint()
end

return true
end

ball:addEventListener(“touch”, removeJ)
bg:addEventListener(“touch”, bounce)

[/code] [import]uid: 10542 topic_id: 7228 reply_id: 307228[/import]

The code seems to work ok for me. The joint gets removed the first time I click on the ball. The second I click on it I do get an error about removeSelf() being a nil value, but that’s because the joint was removed on the prior touch. For instance, I don’t get any error after adding a nil check.

local function removeJoint (event)  
 if myJoint ~= nil then  
 myJoint:removeSelf()  
 myJoint = nil  
 end  
end  

Tim [import]uid: 8196 topic_id: 7228 reply_id: 26158[/import]