I need some help understanding this. The code below works correctly at first. Touch (click) the ball and you can drag it around the white testGroup. But if you uncomment lines 58, 59 to shift the testGroup, it stops working correctly when you click and drag the ball.
I assume this has something to do with stage coordinates vs local coordinates. but I don’t really understand how to use them to solve this problem. Can anyone help?
[code]
– turn off status display bar
display.setStatusBar(display.HiddenStatusBar)
–Physics
local physics = require(“physics”);
physics.start()
physics.setGravity(0, 0)
local bg = display.newGroup() – Create a background group
local testGroup = display.newGroup() – Create a game group
bg:insert( testGroup )
local rect = display.newRect( 0, 0, 300, 300 ) – Create a rectangle object
testGroup:insert( rect ) – add rect to group
local ball = {}
function dragBall(e)
local body = e.target;
local phase = e.phase;
local stage = display.getCurrentStage();
if(phase == “began”) then
–Focus the touch to ONLY its target
stage:setFocus(body, e.id)
– Create a temporary touch joint and store it in the object for later reference
body.tempJoint = physics.newJoint(“touch”, body, e.x, e.y)
elseif(phase == “moved”) then
– Update the joint to track the touch
body.tempJoint:setTarget(e.x, e.y);
elseif(phase == “ended” or phase == “cancelled”) then
stage:setFocus(body, nil);
– Remove the joint when the touch ends
body.tempJoint:removeSelf();
end
– Stop further propagation of touch event
return true
end
local doBall = function(xBase, yBase, radius)
ball = display.newCircle(xBase, yBase, radius )
ball:setFillColor(255,0,0)
testGroup:insert(ball)
physics.addBody(ball, “dynamic”, {density = 5.0, friction = 0, bounce = 0, radius = radius})
ball:addEventListener(“touch”, dragBall);
end
doBall(50,50,20)
–testGroup:setReferencePoint(display.CenterReferencePoint)
–testGroup.x , testGroup.y = display.contentCenterX, display.contentCenterY
[/code] [import]uid: 23636 topic_id: 26827 reply_id: 326827[/import]