Lua – Drag Physical Objects in Corona SDK

I am trying to drag a dynamic object with gravity = 0,0 in my scene. I have a dynamic square and a static image, but when on the image There is a little power when dragging the square, but it can exceed the image and be transferred to the other side like an image:

This is my code for dragging the square:

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.

When you manually move an object, you are not under the physical control of doing so, basically you can force The object moves past the stationary object.

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.

WordPress database error: [Table 'yf99682.wp_s6mz6tyggq_comments' doesn't exist]
SELECT SQL_CALC_FOUND_ROWS wp_s6mz6tyggq_comments.comment_ID FROM wp_s6mz6tyggq_comments WHERE ( comment_approved = '1' ) AND comment_post_ID = 3208 ORDER BY wp_s6mz6tyggq_comments.comment_date_gmt ASC, wp_s6mz6tyggq_comments.comment_ID ASC

Leave a Comment

Your email address will not be published.