Hi,
I changed my config.lua according to the latest tutorial by Rob Miracle to fit my needs.
Now, I want to use the Pinch Zoom XL code to zoom in and out of the screen. I changed the code, regarding the comments to prevent showing black area to the player and also, made a little change to let it work for all the group(just changed the image:touch to levelgroup:touch).
It worked perfectly when I stretched the image but now I want to prevent the user from seeing black bars and bleed areas but I can’t seem to find a way to modify the code to work this way.
It’s a bit long, sorry for that but I’m not sure how to fix my problem so here is the latest version for the Pinch Zoom XL:
[lua]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
local function calculateCenter( previousTouches, event )
local id,touch = next( previousTouches )
if event.id == id then
id,touch = next( previousTouches, id )
assert( id ~= event.id )
end
local cx = math.floor( ( touch.x + event.x ) * 0.5 )
local cy = math.floor( ( touch.y + event.y ) * 0.5 )
return cx, cy
end
function levelGroup:touch(event)
if (not touchActive) then
local phase = event.phase
local eventTime = event.time
local previousTouches = self.previousTouches
if not self.xScaleStart then
self.xScaleStart, self.yScaleStart = self.xScale, self.yScale
end
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
– Very first “began” event
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
– Store initial position
self.x0 = event.x - self.x
self.y0 = event.y - self.y
previousTouches = {}
self.previousTouches = previousTouches
self.numPreviousTouches = 0
self.firstTouch = event
elseif not self.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
self.distance = d
self.xScaleOriginal = self.xScale
self.yScaleOriginal = self.yScale
self.x0 = cx - self.x
self.y0 = cy - self.y
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
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 / self.distance
if ( scale > 0 and self.xScaleOriginal * scale <= 1.5 and self.xScaleOriginal * scale >= 1) then
self.xScale = self.xScaleOriginal * scale
self.yScale = self.yScaleOriginal * scale
self.x = cx - ( self.x0 * scale )
if self.x > 0 then
self.x = 0
elseif (self.x + self.width * self.xScale) < display.contentWidth then
self.x = display.contentWidth - self.width * self.xScale
end
self.y = cy - ( self.y0 * scale )
if self.y > 0 then
self.y = 0
elseif (self.y + self.height * self.yScale) < display.contentHeight then
self.y = display.contentHeight - self.height * self.yScale
end
end
end
else
if event.id == self.firstTouch.id then
– don’t move unless this is the first touch id.
– Make object move (we subtract self.x0, self.y0 so that moves are
– relative to initial grab point, rather than object “snapping”).
self.x = event.x - self.x0
if self.x > 0 then
self.x = 0
elseif (self.x + self.width * self.xScale) < display.contentWidth then
self.x = display.contentWidth - self.width * self.xScale
end
self.y = event.y - self.y0
if self.y > 0 then
self.y = 0
print “ready”
elseif (self.y + self.height * self.yScale) < display.contentHeight then
self.y = display.contentHeight - self.height * self.yScale
end
end
end
if event.id == self.firstTouch.id then
self.firstTouch = event
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 self.numPreviousTouches == 1 then
– must be at least 2 touches remaining to pinch/zoom
self.distance = nil
– reset initial position
local id,touch = next( previousTouches )
self.x0 = touch.x - self.x
self.y0 = touch.y - self.y
self.firstTouch = touch
elseif self.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 )
self.isFocus = false
self.distance = nil
self.xScaleOriginal = nil
self.yScaleOriginal = nil
– reset array
self.previousTouches = nil
self.numPreviousTouches = nil
end
end
end
end
return true
end[/lua] [import]uid: 191500 topic_id: 33713 reply_id: 333713[/import]