Hey guys,
I am using the code from the “Pinch Zoom XL” that has been shared by the community. This code does everything I want except that it does not support rotating objects with two fingers. I am attempting to get this to work and ask for your help. I am a noob at touch stuff so I have spent a few days on this and can’t figure it out. Here is my code… (emphasis on line 96)
[lua]function pinchTouch( event )
local t = event.target
lastTouchedObject = event.target
local phase = event.phase
local eventTime = event.time
local previousTouches = t.previousTouches
if not t.xScaleStart then
t.xScaleStart, t.yScaleStart = t.xScale, t.yScale
end
local numTotalTouches = 1
if previousTouches then
– add in total from previousTouches, subtract one if event is already in the array
numTotalTouches = numTotalTouches + t.numPreviousTouches
if previousTouches[event.id] then
numTotalTouches = numTotalTouches - 1
end
end
if “began” == phase then
– Very first “began” event
if not t.isFocus then
– Subsequent touch events will target button even if they are outside the contentBounds of button
display.getCurrentStage():setFocus( t )
t.isFocus = true
t:toFront()
if global.builderMode == true then
transition.to(deleteStickerButton, {time=600, alpha = 1})
end
– Store initial position
t.x0 = event.x - t.x
t.y0 = event.y - t.y
previousTouches = {}
t.previousTouches = previousTouches
t.numPreviousTouches = 0
t.firstTouch = event
elseif not t.distance then
local dx,dy
local cx,cy
if previousTouches and numTotalTouches >= 2 then
dx,dy = calculateDelta( previousTouches, event )
cx,cy = calculateCenter( previousTouches, event )
end
– initialize to distance between two touches
if dx and dy then
local d = math.sqrt( dx*dx + dy*dy )
if d > 0 then
t.distance = d
t.xScaleOriginal = t.xScale
t.yScaleOriginal = t.yScale
t.x0 = cx - t.x
t.y0 = cy - t.y
end
end
end
if not previousTouches[event.id] then
t.numPreviousTouches = t.numPreviousTouches + 1
end
previousTouches[event.id] = event
elseif t.isFocus then
if “moved” == phase then
if t.distance then
local dx,dy
local cx,cy
if previousTouches and numTotalTouches == 2 then
dx,dy = calculateDelta( previousTouches, event )
cx,cy = calculateCenter( previousTouches, event )
end
if dx and dy then
local newDistance = math.sqrt( dx*dx + dy*dy )
local scale = newDistance / t.distance
if scale > 0 then
t.xScale = t.xScaleOriginal * scale
t.yScale = t.yScaleOriginal * scale
–THIS IS THE LINE IM WORKING WITH
t.rotation = math.atan2( dy, dx ) * 180 / math.pi
– Make object move while scaling
–t.x = cx - ( t.x0 * scale )
–t.y = cy - ( t.y0 * scale )
end
end
else
if event.id == t.firstTouch.id then
– don’t move unless this is the first touch id.
– Make object move (we subtract t.x0, t.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
t.x = event.x - t.x0
t.y = event.y - t.y0
end
end
if event.id == t.firstTouch.id then
t.firstTouch = event
end
if not previousTouches[event.id] then
t.numPreviousTouches = t.numPreviousTouches + 1
end
previousTouches[event.id] = event
elseif “ended” == phase or “cancelled” == phase then
print(t.parent.name)
if t.parent.name == “charactersGUI” then
tempCharacterGroup:remove(t)
objectGroup:insert(t)
end
– check for taps
local dx = math.abs( event.xStart - event.x )
local dy = math.abs( event.yStart - event.y )
if eventTime - previousTouches[event.id].time < 150 and dx < 10 and dy < 10 then
if not t.tapTime then
– single tap
t.tapTime = eventTime
t.tapDelay = timer.performWithDelay( 300, function() t.tapTime = nil end )
elseif eventTime - t.tapTime < 300 then
– double tap
timer.cancel( t.tapDelay )
t.tapTime = nil
–[[ Use below code for double tap to auto resize stickers
if t.xScale == t.xScaleStart and t.yScale == t.yScaleStart then
transition.to( t, { time=300, transition=easing.inOutQuad, xScale=t.xScale*2, yScale=t.yScale*2, x=event.x - t.x0*2, y=event.y - t.y0*2 } )
else
local factor = t.xScaleStart / t.xScale
transition.to( t, { time=300, transition=easing.inOutQuad, xScale=t.xScaleStart, yScale=t.yScaleStart, x=event.x - t.x0*factor, y=event.y - t.y0*factor } )
end
–]]
–Mirror sticker on double tap
t:scale(-1, 1)
end
end
–
if previousTouches[event.id] then
t.numPreviousTouches = t.numPreviousTouches - 1
previousTouches[event.id] = nil
end
if t.numPreviousTouches == 1 then
– must be at least 2 touches remaining to pinch/zoom
t.distance = nil
– reset initial position
local id,touch = next( previousTouches )
t.x0 = touch.x - t.x
t.y0 = touch.y - t.y
t.firstTouch = touch
elseif t.numPreviousTouches == 0 then
– previousTouches is empty so no more fingers are touching the screen
– Allow touch events to be sent normally to the objects they “hit”
display.getCurrentStage():setFocus( nil )
t.isFocus = false
t.distance = nil
t.xScaleOriginal = nil
t.yScaleOriginal = nil
– reset array
t.previousTouches = nil
t.numPreviousTouches = nil
end
if global.builderMode == true then
if (event.x > (deleteStickerButton.x - garbageRadius) and event.x < (deleteStickerButton.x + garbageRadius)) and (event.y > (deleteStickerButton.y - garbageRadius) and event.y < (deleteStickerButton.y + garbageRadius)) then
deleteSticker()
end
transition.to(deleteStickerButton, {time=600, alpha = 0})
end
end
end
return true
end[/lua]
If you look at line 96, you will see “t.rotation = math.atan2( dy, dx ) * 180 / math.pi”
This somewhat works, but the objects’ rotation jerks around unexpectedly. Again I have a hard time understanding all this code, I am not even sure that I should be working with “dy” and “dx”. Any help would be greatly appreciated [import]uid: 19620 topic_id: 31741 reply_id: 331741[/import]