This is my code for dragging the square:
p>
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
body.tempJoint = physics.newJoint( "touch", body, event.x, event.y )
elseif body.isFocus then
if "moved" == phase then
body.tempJoint:setTarget( event.x, event.y )
elseif "ended" == phase or "cancelled" == phase then
stage:setFocus( body, nil )
body.isFocus = false
body .tempJoint:removeSelf()
end
end
return true
end
This is the code to create the object:
function scene:createScene( event )
local group = self.view
my_square = display.newImage("square.png")
my_square.x = 60
my_square.y = 60
physics.addBody(my_square, "dynamic" )
group:insert(my_square)
floor = display.newImage("piso.png")
floor.x = 160< br /> floor.y = 240
physics.addBody(floor, "static" )
group:insert(floor)
end
Thanks for the help.
What you can do is to set up collision detection, when you move the square, an event will be given to you, telling you when to stop moving. Of course, if you don’t follow the movement Code, you can continue to move your object.
I am trying to drag a dynamic object with gravity = 0,0 in my scene, and I have a dynamic body An image of a square and a static body shape, but when dragging the square on the image, this has a little power but can exceed the image and be transferred to the other side like an image:
This is my drag Code of moving block:
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
body.tempJoint = physics.newJoint( "touch", body, event.x, event.y )
elseif body.isFocus then
if "moved" = = phase then
body.tempJoint:setTarget( event.x, event.y )
elseif "ended" == phase or "cancelled" == phase then
stage: setFocus( body, nil )
body.isFocus = false
body.tempJoint:removeSelf()
end
end
return true
end
This is the code to create the object:
function scene:createScene( event )
local group = self.view
my_square = display.newImage("square.png")
my_square.x = 60
my_square.y = 60
phy sics.addBody(my_square, "dynamic" )
group:insert(my_square)
floor = display.newImage("piso.png")
floor.x = 160< br /> floor.y = 240
physics.addBody(floor, "static" )
group:insert(floor)
end
Thanks for the help.
When you manually move an object, you are not under the physical control of doing so, basically you can force the object to move past a stationary object.
What you can do is set up collision detection. When you move the square, it will give you an event telling you when to stop moving. Of course, if you don’t follow the movement code, you can continue to move your object. p>