You see I moved the “game” displayGroup that contains all the physicsbodies that need to interact with each other up 40 pixels to create a scrolling effect and reveal the next portion of the level.
game:contentToLocal(x,y) works great for adjusting everything in the touch event handler, except the physics joints, specifically the touch joint. The hybrid view shows the joint still floating 40 pixels above where I told them to go. touch events got adjusted to the right spot using that approach, and everything but the physics joints. Everything would be exactly correct if the “game” group was back at the 0,0 position.
I even tried hard-coding an offset of 40 pixels. It just moves the physicsbody that far and the touch joint is still hovering off-set by the same amount that I scrolled the “game” group.
Am I just seeing a glitch and the joints are really where they belong, just being hybrid-drawn in the wrong place?
Sorry if I gave double Xs instead of X,Y in some places, or if dynamicimage has caps in the wrong places. I had to replace the name to hide the nature of the game (we want to make money off of it).
local function touch(event, dynamicImage, params)
if (gameManager.isClearingLevel) then
return
end
local grabberHelper = event.target
local phase = event.phase
local stage = display.getCurrentStage()
local adjustedEventX
local adjustedEventY
adjustedEventX, adjustedEventY = gameManager.game:contentToLocal(event.x, event.y)
local adjustedimageX
local adjustedimageY
adjustedimageX, adjustedimageY = gameManager.game:contentToLocal(dynamicImage.x, dynamicImage.y)
local adjustedGrabberX
local adjustedGrabberY
adjustedGrabberX, adjustedGrabberY = gameManager.game:contentToLocal(grabberHelper.x, grabberHelper.y)
if (“began” == phase) then
stage:setFocus( grabberHelper, event.id )
grabberHelper.isFocus = true
– Create a temporary touch joint and store it in the object for later reference
if (params and params.center) then
– drag the dynamicImage from its center point
dynamicImage.tempJoint = physics.newJoint( “touch”, dynamicImage, adjustedimageX, adjustedimageY )
else
– drag the dynamicImage from the point where it was touched
dynamicImage.tempJoint = physics.newJoint( “touch”, dynamicImage, adjustedEventX, adjustedEventY)
end
– grab the initial position
–doesn’t do anything in the current code: dynamicImage.initialPositions = {x = adjustedEventX - adjustedGrabberX, y = adjustedEventY - adjustedGrabberY, event = event}
– 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
dynamicImage.tempJoint.maxForce = params.maxForce
end
if (params.frequency) then
– This is the response speed of the elastic joint: higher numbers = less lag/bounce
dynamicImage.tempJoint.frequency = params.frequency
end
if (params.dampingRatio) then
– Possible values: 0 (no damping) to 1.0 (critical damping)
dynamicImage.tempJoint.dampingRatio = params.dampingRatio
end
end
elseif (grabberHelper.isFocus) then
if (“moved” == phase) then
– Update the joint to track the touch
dynamicImage.tempJoint:setTarget( adjustedEventX, adjustedEventY)
elseif (“ended” == phase or “cancelled” == phase) then
stage:setFocus( grabberHelper, nil )
grabberHelper.isFocus = false
grabberHelper.x, grabberHelper.y = adjustedimageX, adjustedimageY
grabberHelper.rotation = dynamicImage.rotation
– Remove the joint when the touch ends
if (dynamicImage and dynamicImage.tempJoint) then
display.remove(dynamicImage.tempJoint)
end
end
end
– Stop further propagation of touch event
return true
end
M.touch = touch [import]uid: 63787 topic_id: 31335 reply_id: 331335[/import]