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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | -- 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) |
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.
1 2 3 4 5 6 | local function removeJoint (event) if myJoint ~= nil then myJoint:removeSelf() myJoint = nil end end |