2017-07-17 10 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と働いています:) –

関連する問題