@ericdg123, that drag function came from the gameUI library that’s in some of the sample code. I was looking at that too and it looks a little broken, since you can’t actually pass a params argument to the listener function when the event occurs.
One solution is to create a closure. Below is a modification of Tim’s example. You have a function (createDragBody) that creates another function (dragBody) that you pass as the listener. The created function (dragBody) is a closure b/c it stores values you passed to createDragBody. In the example below, I set the dampingRatio to 1.0:
[lua]–> Setup Display
display.setStatusBar (display.HiddenStatusBar)
–> Start Physics
local physics = require (“physics”)
physics.start ()
physics.setGravity (0, 10)
–physics.setDrawMode (“hybrid”)
–> Create Walls
local leftWall = display.newRect (0, 0, 1, display.contentHeight)
local rightWall = display.newRect (display.contentWidth, 0, 1, display.contentHeight)
local ceiling = display.newRect (0, 0, display.contentWidth, 1)
local floor = display.newRect (0, display.contentHeight, display.contentWidth, 1)
physics.addBody (leftWall, “static”, {bounce = 0.0, friction = 10})
physics.addBody (rightWall, “static”, {bounce = 0.0, friction = 10})
physics.addBody (ceiling, “static”, {bounce = 0.0, friction = 10})
physics.addBody (floor, “static”, {bounce = 0.0, friction = 10})
local xCenter = 160
local wCeil = 120
local hCeil = 20
local ceiling = display.newRect( xCenter - wCeil*0.5, 0, wCeil, hCeil )
physics.addBody( ceiling, “static”, { density=0, friction=0.5,bounce=0.2 } )
local prevBody = ceiling
local w,h = 10,50
local halfW,halfH = 0.5*w,0.5*h
– center of body
local x = xCenter
local y = hCeil - halfH
local yJoint = y - halfH
– rope
for i = 1, 5 do
y = y + h
yJoint = yJoint + h
local body = display.newRect( x-halfW, y-halfH, w, h )
body:setFillColor( 255, 0, 0, 128 )
physics.addBody( body, { density=50, friction=0.5, bounce=.2 })
local joint = physics.newJoint( “pivot”, prevBody, body, xCenter, yJoint )
prevBody = body
end
– final body
y = y + halfH
local r = h*0.5
local body = display.newCircle( x, y, r )
body:setFillColor( 0, 0, 255, 128 )
physics.addBody( body, { density=2, friction=0.5, bounce=.2, radius=r })
local joint = physics.newJoint( “pivot”, prevBody, body, xCenter, y )
local ball = body
local function createDragBody( params )
local function dragBody( event )
local body = event.target
local phase = event.phase
local stage = display.getCurrentStage()
if “began” == phase then
stage:setFocus( body, event.id )
body.isFocus = true
– 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.maxForce 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
print( “ratio”, params.dampingRatio )
– Possible values: 0 (no damping) to 1.0 (critical damping)
body.tempJoint.dampingRatio = params.dampingRatio
end
end
elseif body.isFocus then
if “moved” == phase then
– Update the joint to track the touch
body.tempJoint:setTarget( event.x, event.y )
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
return dragBody
end
local dragBody = createDragBody( { dampingRatio = 1.0 })
ball:addEventListener ( “touch”, dragBody )[/lua] [import]uid: 26 topic_id: 3208 reply_id: 20523[/import]