I have a right scrolling game and my clicking and dragging using the sample code for the mouse joint works well, but as the player progresses to the right (stage/level:setX()), I get odd behavior.
the sample code:
-- create a mouse joint on mouse down
function launcher:grabber_display_on_down(event)
if self.grabber_display:hitTestPoint(event.touch.x, event.touch.y) then
local jointDef = b2.createMouseJointDef(self.ground, self.grabber_body, event.touch.x, event.touch.y, 5500, 10, 10)
self.mouseJoint = world:createJoint(jointDef)
end
end
-- update the target of mouse joint on mouse move
function launcher:grabber_display_on_move(event)
if self.mouseJoint ~= nil then
self.mouseJoint:setTarget(event.touch.x, event.touch.y)
end
end
-- destroy the mouse joint on mouse update up
function launcher:grabber_display_on_up(event)
if self.mouseJoint ~= nil then
world:destroyJoint(self.mouseJoint)
self.mouseJoint = nil
end
end |
--------------------
After I move the stage to the left, how can I get the mouse joint to work properly? I've tried keeping a variable of how far the stage has moved 'screen_current_x = 0' and when the stage moves I update that and changed the code above to:
function launcher:grabber_display_on_down(event)
if self.grabber_display:hitTestPoint(event.touch.x, event.touch.y) then
local x = event.touch.x + screen_current_x
local jointDef = b2.createMouseJointDef(self.ground, self.grabber_body, x, event.touch.y, 5500, 10, 10)
self.mouseJoint = world:createJoint(jointDef)
end
end |
-------------------
Not sure if that makes sense. Hopefully, I explained my problem well. Anyone know how to fix this?
Comments
Fragmenter - animated loop machine and IKONOMIKON - the memory game
Here is the code for moving the level (if it's needed). The level is just a Sprite instance.
Likes: Apollo14, antix