Hi there,
From what I understand there are no methods to alter existing physics body properties during runtime, so I’m left with removing the body and adding it back with the altered properties. After a certain time, I’d like these properties to return to normal, so I remove and add the physics body again. However this brings up issues when the physics body is removed during a touch event (dragBody).
I’m looking for a way to cancel out of this while the physics body is being switched out. Any ideas? This is the drag function I’m working with from the gameUI library.
function dragBody( event, params ) local body = event.target local phase = event.phase local stage = display.getCurrentStage() if "began" == phase then stage:setFocus( body, event.id ) body.isFocus = true if params.canTouch == false then phase = "cancelled" circle = display.newCircle(300,300,300) body.tempJoint:removeSelf() canTouch = false return true else circle = {} end -- Create a temporary touch joint and store it in the object for later reference if params and params.center then -- drag the body from its center point body.tempJoint = physics.newJoint( "touch", body, body.x, body.y ) else -- drag the body from the point where it was touched body.tempJoint = physics.newJoint( "touch", body, event.x, event.y ) end -- Apply optional joint parameters if params then local maxForce, frequency, dampingRatio if params.g then -- Internal default is (1000 \* mass), so set this fairly high if setting manually body.tempJoint.maxForce = params.maxForce end if params.frequency then -- This is the response speed of the elastic joint: higher numbers = less lag/bounce body.tempJoint.frequency = params.frequency end if params.dampingRatio then -- Possible values: 0 (no damping) to 1.0 (critical damping) body.tempJoint.dampingRatio = params.dampingRatio end end elseif body.isFocus then if params.canTouch == false then phase = "cancelled" circle = display.newCircle(300,300,300) return true end if "moved" == phase then if params.canTouch == false then phase = "cancelled" circle = display.newCircle(300,300,300) body.tempJoint:removeSelf() return true else -- Update the joint to track the touch body.tempJoint:setTarget( event.x, event.y ) end elseif "ended" == phase or "cancelled" == phase then stage:setFocus( body, nil ) body.isFocus = false -- Remove the joint when the touch ends body.tempJoint:removeSelf() end end -- Stop further propagation of touch event return true end