如何在 Corona SDK 中检查滑动的角度

How to check the angle of a swipe in corona 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)

当事件阶段为 "ended" 时,我还会检查 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 更改为适合您的值。