2011-07-05 82 views
0

我已經基本搜索「無處不在」,並不能找到一個解決方案,因此,我在這裏和希望得到任何幫助縮放比例限制,我試圖限制這種代碼的最大/最小規模,截至目前,你可以捏和縮放到無窮遠,我想設置多遠,用戶可以放大和縮小的限制,所以我將如何添加到該代碼捏在Lua科羅娜SDK

-- activate multitouch system.activate("multitouch")

 -- add bkgd image to screen 
local background = display.newImage("aquariumbackgroundIPhone.jpg", 0, 0) 

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 

-- create a table listener object for the bkgd image 
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 stageBounds 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 
    end 

    -- Determine if running on Corona Simulator 
     -- 
    local isSimulator = "simulator" == system.getInfo("environment") 

    -- Multitouch Events not supported on Simulator 
     -- 
     if isSimulator then 
     msg = display.newText("Multitouch not supported on Simulator!", 0, 20,  "Verdana-Bold", 14) 
     msg.x = display.contentWidth/2   -- center title 
      msg.y = display.contentHeight/2   -- center title 
      msg:setTextColor(255,255,0) 
      end 

      -- register table listener 
      background:addEventListener("touch", background) 
+1

那功能......可怕的長。你可以刪除不手頭的問題來說重要的部分? – kikito

回答

4

在這裏,你做的邏輯:

if (scale > 0) then
self.xScale = self.xScaleOriginal * scale
self.yScale = self.yScaleOriginal * scale
end

這走的是原規模,並通過新的比例相乘。所以,你需要做的是這樣的:

當地xScale等= self.xScaleOriginal *規模
當地yScale = self.yScaleOriginal *規模

//設置上限
xScale等= math.min(ZOOMMAX,xScale等)
yScale = math.min(ZOOMMAX,yScale)

//設置下限
self.xScale = math.max(ZOOMMIN,xScale等)
self.yScale = math.max(ZOOMMIN,yScale) `