2015-02-23 128 views
0

我使用包含在電暈示例代碼中捏放大縮小的功能。但是,因爲我正在開發一個應用程序,所以我需要讓每個感興趣的地方變得可以縮放。我是否必須複製和粘貼每個圖像的可怕長時間的功能,還是隻能將一個功能的每個圖像重新定向?我是新來的盧阿,這裏是變焦功能捏包含在示例代碼:電暈sdk捏縮放

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 
    -- 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 

     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 
        self.xScale = self.xScaleOriginal * scale 
        self.yScale = self.yScaleOriginal * scale 
       end 
      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 

回答

0

你也許可以使用closure做到這一點,假設所有的興趣點都是一樣的。

實施例:

local function createPOI(properties) 
    local pointOfInterest = createPointOfInterest() -- code to create your object 
    --set properties based on argument 
    function pointOfInterest:touch(e) 
     --do stuff 
    end 
    pointOfInterest:addEventListner("touch") 
    return pointOfInterest 
end 

電暈有一些代碼here可能是有用的。檢查'多個接觸'部分。