Dragging functionality

Hi,

I’m new to Corona and I was trying to make an image on the screen to be draggable no matter where you touch the screen (you don’t necessarily need to touch this image inside)

Any hint on how I can achieve this?

Thank you! [import]uid: 8241 topic_id: 10147 reply_id: 310147[/import]

One way is to have a full screen box set to alpha=.01 that is the actual draggable object and then make the image follow that object as you drag it. That way you dont have to tap and drag on the actual object.

Do you want the image to “snap” to your finger location? That too is possible.
[import]uid: 7845 topic_id: 10147 reply_id: 37028[/import]

Thank you for your reply, but I can’t figure it out how to make it work.

I have this code:

local physics = require("physics")  
physics.start()  
  
local function startDrag( event )  
 local t = event.target  
  
 local phase = event.phase  
 if "began" == phase then  
 display.getCurrentStage():setFocus( t )  
 t.isFocus = true  
  
 -- Store initial position  
 t.x0 = event.x - t.x  
 t.y0 = event.y - t.y  
  
 -- Make body type temporarily "kinematic" (to avoid gravitional forces)  
 event.target.bodyType = "kinematic"  
  
 -- Stop current motion, if any  
 event.target:setLinearVelocity( 0, 0 )  
 event.target.angularVelocity = 0  
  
 elseif t.isFocus then  
 if "moved" == phase then  
 t.x = event.x - t.x0  
 t.y = event.y - t.y0  
  
 elseif "ended" == phase or "cancelled" == phase then  
 display.getCurrentStage():setFocus( nil )  
 t.isFocus = false  
  
 -- Switch body type back to "dynamic", unless we've marked this sprite as a platform  
 if ( not event.target.isPlatform ) then  
 event.target.bodyType = "dynamic"  
 end  
  
 end  
 end  
  
 -- Stop further propagation of touch event!  
 return true  
end  
  
local ball = display.newImage( "images/ball.png" )  
ball.x = 40; ball.y = 280  
physics.addBody( ball, "kinematic", { friction=0.7 } )  
ball.isPlatform = true -- custom flag, used in drag function above  
  
ball:addEventListener( "touch", startDrag )  

but I want to touch around the ball and be draggable not on it’s surface only. how should I alter this? [import]uid: 8241 topic_id: 10147 reply_id: 37122[/import]

Try this…

[lua]local physics = require(“physics”)
physics.start()

local ball = display.newImage( “images/ball.png” )
ball.x = 40; ball.y = 280
physics.addBody( ball, “kinematic”, { friction=0.7 } )

local function startDrag( event )

local phase = event.phase
if “began” == phase then

– Store initial position
x0 = event.x
y0 = event.y

ballx0 = ball.x
bally0 = ball.y

print(x0, y0)

– Stop current motion, if any
ball:setLinearVelocity( 0, 0 )
ball.angularVelocity = 0

elseif “moved” == phase or “ended” == phase or “cancelled” == phase then
ball.x = ballx0 + event.x - x0
ball.y = bally0 + event.y - y0

end

– Stop further propagation of touch event!
return true
end

Runtime:addEventListener( “touch”, startDrag )[/lua]
[import]uid: 48521 topic_id: 10147 reply_id: 37132[/import]

assuming your object “obj” is already created you could do something like this. Obviously, you will need to handle physics seperately if it’s physics object
****EDIT**** see my next post… :slight_smile: [import]uid: 48521 topic_id: 10147 reply_id: 37091[/import]

Thank you so much!

This worked like a charm.

I’m looking for someone to show me a couple of things in Corona, are you available(I will pay you, of course)? [import]uid: 8241 topic_id: 10147 reply_id: 37338[/import]

Haha thanks for offer. I will help wherever I can. Not sure if I want payment for sharing basic knowledge. :slight_smile:

Most ppl are helpful here. Just avoid asking questions that have been already answered. Make sure you search forums before you ask.

Some of us are also on IRC chat where you can ask for help. Do drop by there. [import]uid: 48521 topic_id: 10147 reply_id: 37367[/import]