2016-10-22 2 views
0

내 게임에서 나는 터치 이벤트를 사용하여 객체를 제어합니다. 화면의 오른쪽 절반을 만질 때, 물체가 회전하고 화면의 왼쪽 절반을 만질 때 물체가 움직입니다. 그것은 단 한 번의 터치 일 때 완벽하게 작동하지만, 화면의 어느 한면을 터치하고 동시에 다른면을 터치하기 시작하면 예기치 못한 혼합 된 동작이 발생합니다.다중 터치 감지

내 질문은 분리 또는 여러 터치를 구별하는 방법입니다.

system.activate("multitouch") 

    onTouch = function (event) 

    if (event.phase == "began") then 
     pX = event.x  -- Get start X position of the touch 
     print("ID:"..tostring(event.id)) 
     if (event.x > centerX) then  --if the touch is in the right or left half of the screen 
      xPos = "right" 
     else 
      xPos = "left" 
     end 

    elseif (event.phase == "moved") then 
     local dX = (event.x - pX) 
     if (xPos == "right") then 
      rotatePlayer(dx) 
     else 
      movePlayer(dX) 
    end 

업데이트 :

system.activate("multitouch") 

local touchID = {}   --Table to hold touches 

onTouch = function (event) 

    if (event.phase == "began") then 

     print("ID:"..tostring(event.id)) 
     if (event.x > centerX) then  --if the touch is in the right or left half of the screen 
      touchID[event.id] = {} 
      touchID[event.id].x = event.x 
      xPos = "right" 
      pX = touchID[event.id].x  -- Get start X position of the touch 
     else 
      touchID[event.id] = {} 
      touchID[event.id].x = event.x 
      xPos = "left" 
      pX = touchID[event.id].x 
     end 

    elseif (event.phase == "moved") then 
     print("ID:"..tostring(event.id)) 

     local dX 
     if (xPos == "right") then 
      touchID[event.id].x = event.x 
      dX = touchID[event.id].x - pX 
      rotatePlayer(dx) 
     else 
      touchID[event.id].x = event.x 
      dX = touchID[event.id].x - pX 
      movePlayer(dX) 
    end 

같은 문제는 여전히 존재합니다.

답변

0

event.id 필드를 무시하는 것처럼 보입니다. 그렇기 때문에 여러 건드리기 동작이 섞여있는 것입니다.

began 단계가되면 일부 목록에 새로운 터치를 저장하여 추적하십시오. 터치 초기 좌표 (귀하의 pX가 있음)와 나중에 필요할 수도있는 것을 포함하십시오. 다른 이벤트 (이동/종료/취소)가 발생하면 활성 터치 목록을 확인하고 실제 터치를 event.id까지 찾아 정확한 터치에 대한 논리를 수행해야합니다.

+0

터치 ID를 테이블에 추가하고 터치로 이동 시키려고했지만 동일한 문제가 여전히 존재합니다. 업데이트 된 질문을 확인하십시오. – Abdou023

0

아직 데이터를 섞어 가고 있습니다. xPos은 터치 기능이므로 터치 이벤트에 저장해야하며 다른 터치의 데이터로 업데이트되는 전역 변수에 저장되어서는 안됩니다.

또한 중복 된 행을 if 브랜치 밖으로 이동하면 모두 동일합니다. 코드가 더 간단 해지고 읽고 이해하기가 훨씬 쉬워집니다.

system.activate("multitouch") 

local touchID = {}   --Table to hold touches 

onTouch = function (event) 
    local x, id, phase = event.x, event.id, event.phase 
    print("ID:"..tostring(id)) 

    if (phase == "began") then 
     touchID[id] = { 
      x = x, 
      logic = (x > centerX) and rotatePlayer or movePlayer 
     } 
    elseif (phase == "moved") then 
     local touch = touchID[id] 
     touch.logic(x - touch.x) 
    end 
end 

"종료/취소됨"단계에서는 여전히 터치를 제거해야합니다.

편집 : 화면의 같은면에 여러 개의 터치가있을 수 있으므로 새로운 터치를 무시하거나 어떻게 든 평균을 보냅니다.