I have a problem, i would like to move an object with a finger. The object is in a group who is can be pan and zoom.
Every things work expect the fact when i move the object it goes to far if the group is zoom and don’t move enought if the group is dezoom.
I write this code with this tuto http://coronalabs.com/blog/2013/01/22/implementing-pinch-zoom-rotate/
I try to resolve this problem during a lot of hours, I hope u can understand the part of my code.
The only solution i found is that: self.x = event.x/(taille^0.55) - self.myX
but that don’t resolve totaly the problem
[lua]
local t = {}
t.zoom = function ()
system.activate( “multitouch” )
local maxpos1, maypos1, maxminuspos1, mayminuspos1,maxminuspos2,mayminuspos2
maxpos1=1800*0.3
maypos1=1024*0.3
maxminuspos2=-2040*0.3
mayminuspos2=-1024*0.3
maxminuspos1=display.actualContentWidth+maxminuspos2
mayminuspos1=display.actualContentHeight+mayminuspos2
local maxpos, maypos, maxminuspos, mayminuspos = maxpos1, maypos1, maxminuspos1, mayminuspos1
local ZOOMMAX,ZOOMMIN= 2.6,0.6
background.x=100
background.y=200
local scaleold=1
local function calculateDelta( previousTouches, event )
local id,touch = next( previousTouches )
if event.id == id then
id,touch = next( previousTouches, id )
assert( id ~= event.id )
end
local dx = touch.x - event.x
local dy = touch.y - event.y
return dx, dy
end
function background:touch( event )
local result = true
local phase = event.phase
local previousTouches = self.previousTouches
local numTotalTouches = 1
if ( previousTouches ) then
– add in total from previousTouches, subtract one if event is already in the array
numTotalTouches = numTotalTouches + self.numPreviousTouches
if previousTouches[event.id] then
numTotalTouches = numTotalTouches - 1
end
end
if “began” == phase then
if ( not self.isFocus ) then
– Subsequent touch events will target button even if they are outside the contentBounds of button
display.getCurrentStage():setFocus( self )
self.isFocus = true
–miko collect values for panning
self.myX = event.x-self.x
self.myY = event.y-self.y
previousTouches = {}
self.previousTouches = previousTouches
self.numPreviousTouches = 0
elseif ( not self.distance ) then
local dx,dy
if previousTouches and ( numTotalTouches ) >= 2 then
dx,dy = calculateDelta( 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
self.distance = d
self.xScaleOriginal = self.xScale
self.yScaleOriginal = self.yScale
print( "distance = " … self.distance )
end
end
end
if not previousTouches[event.id] then
self.numPreviousTouches = self.numPreviousTouches + 1
end
previousTouches[event.id] = event
elseif self.isFocus then
if “moved” == phase then
if ( self.distance ) then
local dx,dy
if previousTouches and ( numTotalTouches ) >= 2 then
dx,dy = calculateDelta( previousTouches, event )
end
if ( dx and dy ) then
local newDistance = math.sqrt( dx*dx + dy*dy )
local scale = newDistance / self.distance
print( “newDistance(” …newDistance … “) / distance(” … self.distance … “) = scale(”… scale …")" )
if ( scale > 0 ) then
–local taille
taille=self.xScaleOriginal*scale
if(taille>= ZOOMMIN and taille<=ZOOMMAX)then
maxpos=maxpos1*taille
maypos=maypos1*taille
maxminuspos=display.actualContentWidth+maxminuspos2*taille
mayminuspos=display.actualContentHeight+mayminuspos2*taille
self.xScale = self.xScaleOriginal * scale
self.yScale = self.yScaleOriginal * scale
if(self.x>maxpos)then
self.x=maxpos
end
if(self.y>maypos)then
self.y=maypos
end
if(self.x<maxminuspos)then
self.x=maxminuspos
end
if(self.y<mayminuspos)then
self.y=mayminuspos
end
end
if(taille<ZOOMMIN)then
taille=ZOOMMIN
end
if(taille>ZOOMMAX)then
taille=ZOOMMAX
end
end
–miko test ind her om xScale < 1 og så juster ved at gange op.
end
else
self.x = event.x - self.myX
self.y = event.y - self.myY
if (self.x > maxpos) then
self.x = maxpos
elseif (self.x < maxminuspos) then
self.x = maxminuspos
end
if (self.y > maypos) then
self.y = maypos
elseif (self.y < mayminuspos) then
self.y = mayminuspos
end
end
if not previousTouches[event.id] then
self.numPreviousTouches = self.numPreviousTouches + 1
end
previousTouches[event.id] = event
elseif “ended” == phase or “cancelled” == phase then
if previousTouches[event.id] then
self.numPreviousTouches = self.numPreviousTouches - 1
previousTouches[event.id] = nil
end
if ( #previousTouches > 0 ) then
– must be at least 2 touches remaining to pinch/zoom
self.distance = nil
else
– 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 )
self.isFocus = false
self.distance = nil
self.xScaleOriginal = nil
self.yScaleOriginal = nil
– reset array
self.previousTouches = nil
self.numPreviousTouches = nil
end
end
end
return result
end
background:addEventListener( “touch”, background )
end
return(t)
the other file:
local function nouv (typebatiment)
numtap=0
function nbatiment:touch( event )
local result = true
local phase = event.phase
local previousTouches = self.previousTouches
local numTotalTouches = 1
if ( previousTouches ) then
numTotalTouches = numTotalTouches + self.numPreviousTouches
if previousTouches[event.id] then
numTotalTouches = numTotalTouches - 1
end
end
if “began” == phase then
if ( not self.isFocus ) then
display.getCurrentStage():setFocus( self )
self.isFocus = true
self.myX = event.x/(taille^0.55)-self.x
self.myY = event.y/(taille^0.55)-self.y
previousTouches = {}
self.previousTouches = previousTouches
self.numPreviousTouches = 0
elseif ( not self.distance ) then
local dx,dy
end
if not previousTouches[event.id] then
self.numPreviousTouches = self.numPreviousTouches + 1
end
previousTouches[event.id] = event
elseif self.isFocus then
if “moved” == phase then
if ( self.distance ) then
local dx,dy
else
self.x = event.x/(taille^0.55) - self.myX
self.y = event.y/(taille^0.55) - self.myY
end
if not previousTouches[event.id] then
self.numPreviousTouches = self.numPreviousTouches + 1
end
previousTouches[event.id] = event
elseif “ended” == phase or “cancelled” == phase then
if previousTouches[event.id] then
self.numPreviousTouches = self.numPreviousTouches - 1
previousTouches[event.id] = nil
end
if ( #previousTouches > 0 ) then
self.distance = nil
else
display.getCurrentStage():setFocus( nil )
if(autorise==0)then
supprimer.isVisible=true
valider.alpha=0.5
valider.isVisible=true
end
self.isFocus = false
self.distance = nil
self.xScaleOriginal = nil
self.yScaleOriginal = nil
self.previousTouches = nil
self.numPreviousTouches = nil
end
end
end
return result
end
[/lua]