2017-07-17 22 views
0

我想要某個功能在人向左或向右滑動時發生,但我不希望該功能在人員沿對角線滑動時執行。有沒有簡單的方法來做到這一點?目前我正在處理以下代碼:如何檢查電暈中的滑動角度SDK

display.setDefault("background", 0.2, 0.2, 0.4) 

local function startDrag(event) 
    local swipeLength = math.abs(event.x - event.xStart) 
    print(event.phase, swipeLength) 
    local t = event.target 
    local phase = event.phase 
    if "began" == phase then 
     return true 
    elseif "moved" == phase then 
    elseif "ended" == phase or "cancelled" == phase then 
     if event.xStart > event.x and swipeLength > 50 then 
      display.setDefault("background", 0/255,3/255, 0/255) 
     elseif event.xStart < event.x and swipeLength > 50 then 
      display.setDefault("background", 0, 0, 1) 
     end 
    end 
end 

local rectangle= display.newRect(100,200,1000,1000) 
rectangle:setFillColor(1,1,1,0.1) 
rectangle:addEventListener("touch",startDrag) 

回答

1

當事件階段「結束」時,我還會檢查y位置。例如,

if (event.y - 5 <= event.yStart and event.y + 5 >= event.yStart) then 

    -- almost perfect line, 
    -- if someone draws a line from bottom-left to top-right, it won't work 
end 

您可以測試並將5或-5更改爲對您有用的東西。

+0

對不起,我寫了{}而不是關鍵字「then」和「end」,我現在正在用JS工作:) –