2013-08-30 94 views
2

我正在嘗試在觸摸事件上設置放大功能。我使用transition.to和onComplete將縮放父組移至事件中心。但之後它只是跳到父組的起源。有任何想法嗎?放大觸摸事件

我現在正在粘貼縮短版本的代碼。

local physics = require("physics") 
physics.start() 
physics.setContinuous(false) 
--physics.setScale(60) 



local height = display.contentHeight 
local width = display.contentWidth 



local backGround = display.newRect(0,0,width,height) 
backGround:setFillColor(91,91,91) 

local allElements = display.newGroup() 
local grip = {} 
local gripSize = { 
-- w: gripwidth, h: gripheight, s: strength required 
{w=30, h=20, s=1}, 
{w=20, h=10, s=1.5}, 
{w=10, h=10, s=2}, 
} 

local r 

local function createTexture() 
    local originX = 0 
    local originY = height -75 
    for i=0,50 do 
     r = math.random(3) 
     local x = originX + math.random(width) 
     local y = originY - math.random(2*height) 
     grip[i] = display.newRect(allElements, x, y, gripSize[r].w, gripSize[r].h) 
     grip[i].size = gripSize[r].s 
     if (r == 1) then 
     grip[i]:setFillColor(51,255,0) 
     elseif (r == 2) then 
     grip[i]:setFillColor(255,51,51) 
     elseif (r == 3) then 
     grip[i]:setFillColor(51,51,255) 
     end 

    end 
end 

createTexture() 



wallBottom = display.newRect(allElements, 0,height-20,width,20) 
physics.addBody(wallBottom, "static", { density=5, friction=0.5, bounce=0.3 }) 

head = display.newCircle(allElements, width/2,50,20) 
physics.addBody(head, { density=5, friction=0.5, bounce=0.3 }) 


local touchBorder = 20 

local function calcTouchOffset(e) 
    local x, y = 0, 0 

    if (e.x < touchBorder) then 
     x = e.x - touchBorder 
    elseif (e.x > width-touchBorder) then 
     x = e.x - (width-touchBorder) 
    end 

    if (e.y < touchBorder) then 
     y = e.y - touchBorder 
    elseif (e.y > height-touchBorder) then 
     y = e.y - (height-touchBorder) 
    end 

    return x, y 
end 


local function startDrag(e) 
    local body = e.target 
    local phase = e.phase 
    local stage = display.getCurrentStage() 

    if (e.phase == "began") then 
     e.target.bodyType = "dynamic" 
     e.target.hasFocus = true 
     local x, y = allElements:contentToLocal(e.x, e.y) 
     e.target.touchjoint = physics.newJoint("touch", e.target, x, y) 
     stage:setFocus(e.target) 
     transition.to(allElements, { time = 200, x= -body.x, y= -body.y, xScale = 2, yScale = 2,}) 
     xOffset, yOffset = 0, 0 
     return true 
    elseif (e.target.hasFocus) then 
     if (e.phase == "moved") then 

      local x,y = allElements:contentToLocal(e.x, e.y)  -- This line is changed 
      e.target.touchjoint:setTarget(x, y)    -- This line is changed 
      xOffset, yOffset = calcTouchOffset(e) 
     else 
      transition.to(allElements, { time = 200, x = body.x, y = body.y, xScale = 1, yScale = 1, }) 
      e.target.hasFocus = false 
      e.target.touchjoint:removeSelf() 
      e.target.touchjoint = nil 
      stage:setFocus(nil) 
      xOffset, yOffset = 0, 0 

     end 
     return true 
    end 
    xOffset, yOffset = 0, 0 
    return false 

end 

head:addEventListener("touch", startDrag) 

    function allElements:update() 


    allElements.x, allElements.y = allElements.x - xOffset, allElements.y - yOffset 
    allElements.x, allElements.y = allElements.x, allElements.y 
    if (allElements.x > -startX) and (startX < 0) then 
     allElements.x = -startX 
    elseif (routeW < width) then 
     allElements.x = 0 
    elseif (allElements.x < startX) and (startX < 0) then 
     allElements.x = startX 
    end 
    --[[if (allElements.x > 0) then 
     allElements.x = 0 
    elseif (routeW < width) then 
     allElements.x = 0  
    elseif (allElements.x < (width-routeW)) and (routeW > width) then 
     allElements.x = width-routeW 
    end ]]--  

    if (allElements.y > (routeH-height)) and (routeH > height) then 
     allElements.y = routeH-height 
    elseif (routeH < height) then 
     allElements.y = 0 
    elseif (allElements.y < 0) then 
     allElements.y = 0 
    end 

end 


function enterFrame() 
    allElements:update() 
end 
+0

你能創建一個完整的例子嗎?你有很多事情沒有關係,如果你只有一個問題需要考慮,那麼調試更容易。你的transition.to touch正在使用'x = -body.x',所以你把'allElements'移到'body.x'單元的左邊。然後在完成時再次使用'x = -body.x',從而將'allElements'再次移動到左側'body.x'單元。此外,在你的觸摸釋放處理程序中,你再次執行一次'transition.to',這將會破壞從初始觸摸開始的任何已經運行的轉換。 – six8

+0

我將創建一個示例。我試圖讓效果像放大觸摸事件發生的區域一樣。這就是爲什麼在「開始」時,我將一個轉換放入組中,並且放在第一組上。 x = -body.x用於聚焦縮放。否則,縮放將從組左上角的點開始。 – jumbee

回答

0

你只需要更新此部分:

elseif (e.target.hasFocus) then 
    if (e.phase == "moved") then 

     local x,y = allElements:contentToLocal(e.x, e.y)  -- This line is changed 
     e.target.touchjoint:setTarget(x, y)    -- This line is changed 
     xOffset, yOffset = calcTouchOffset(e) 
    else 
     transition.to(allElements, { time = 200, x = 0, y = 0, xScale = 1, yScale = 1, }) 
     e.target.hasFocus = false 
     e.target.touchjoint:removeSelf() 
     e.target.touchjoint = nil 
     stage:setFocus(nil) 
     xOffset, yOffset = 0, 0 

    end 
    return true 
end 

通過用x = 0和y = body.y由y = 0

乾杯更換X = body.x :)