Drag + Rotation in 1 function?

Im using startDrag function and rotateObj function (ill post code below) problem is how can I combine both to be able drag object and when you rotate it rotates instead of doing both at once (moved/rotate) I tried setting flags but in the rotate function it never executes the “ended” or “canceled” phase. So the only way I got it to work was to place 2 buttons and 1 activates the drag and other the rotate but I dont want to have 2 buttons on screen. I am thinking maybe the only way would be using multi touch and 1 finger drags and 2 finger rotates? any help is greatly appreciated and if the multi touch is my only option anyone mind sharing some code snippet on how to do this?

Thanks,
LeivaGames

[lua]–================= Drag Function ======================–
–======================================================–

– Drag Function –
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 and 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 = “static”
end
end
end

– Stop further propagation of touch event!
return true
end

– all Objects that will be able to be Dragged

test_object:addEventListener(“touch”, startDrag)
–================ Rotate Function =====================–
–======================================================–

local function rotateObj(event)
local t = event.target
local phase = event.phase

if (phase == “began” ) then
display.getCurrentStage():setFocus( t )
t.isFocus = true

– Store initial position of finger
t.x1 = event.x
t.y1 = event.y

elseif t.isFocus then
if (phase == “moved” ) then
t.x2 = event.x
t.y2 = event.y

angle1 = 180/math.pi * math.atan2(t.y1 - t.y , t.x1 - t.x)
angle2 = 180/math.pi * math.atan2(t.y2 - t.y , t.x2 - t.x)
– print("angle1 = "…angle1)
rotationAmt = angle1 - angle2

–rotate it
t.rotation = t.rotation - rotationAmt
– print ("t.rotation = "…t.rotation)

t.x1 = t.x2
t.y1 = t.y2

elseif (phase == “ended” or “cancelled” == phase ) then

display.getCurrentStage():setFocus( nil )
t.isFocus = false
print(“ENDED”)

end
end

– Stop further propagation of touch event
return true
end

– all Objects that will be able to Rotate

test_object:addEventListener(“touch”, rotateObj)

[import]uid: 30314 topic_id: 21789 reply_id: 321789[/import]

Would anyone be able to implement the Rotation code posted above using 2 finger to rotate it? I guess thats the best way to explain what im trying to accomplish. Because if with 2 fingers you are able rotate the object with the code above then with 1 finger you can drag it.

Thanks,
LeivaGames [import]uid: 30314 topic_id: 21789 reply_id: 88141[/import]

I built a library with example main for doing just that. Check it out here:

https://developer.anscamobile.com/code/multi-point-pinch-zoom-rotate

If you only want to rotate or move just check if the rotate value is changing.
If you don’t want scaling, don’t set those values.

Let me know how it works out for you.

M [import]uid: 8271 topic_id: 21789 reply_id: 88335[/import]

@ horacebury

Thanks for the link and sharing your library ill look into it right now [import]uid: 30314 topic_id: 21789 reply_id: 88421[/import]