如何在 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)
点赞
用户2933219
用户2933219

当事件阶段为“结束”时,我也会检查 y 的位置。例如,

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

   --几乎完美的线,
   --如果有人从左下到右上画一条线,它不起作用
end

您可以测试并更改5或-5以找到适合您的位置。

2017-07-17 14:57:27