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

时间:2017-07-17 09:17:12

标签: lua corona

我希望当一个人向右或向左滑动时发生某个功能,但如果一个人对角滑动手指,我不希望该功能执行。有一个简单的方法吗?目前我正在使用此代码:

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 个答案:

答案 0 :(得分:1)

当事件阶段结束时#34;&#34;我也会检查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更改为适合您的内容。