2016-07-05 5 views
0

나는 문자가있다.왜 SpriteKit 게임에서 동작 논리가 다른가요?

for touch in touches { 
     let location = touch.locationInNode(self) 

     print(location.x) 
     print("Pos \(self.magician.position.x)") 
     if location.x >= self.magician.position.x { 
      print("R") 
      self.movingRight = true 
     } else { 
      print("L") 
      self.movingLeft = true 
     } 
    } 

따라서, 논리입니다 : 다음 location.x 인 경우>, 다음이 의미 내 문자 position.x보다, 내가 오른쪽에 탭과 나의 캐릭터가 이동해야 화면에 탭에 나는이 실행 이 방향으로, 오른쪽으로. 그러나 나는 왜 내가 오른쪽을 탭하면 왼쪽으로 움직이는 지 모르겠다.

SpriteKit에서 논리가 너무 다른 이유를 설명 할 수 있습니까?

답변

1

캐릭터가 SKSpriteNode의 하위 클래스이며이 코드가 캐릭터의 코드에서 실행되고 있다고 생각합니까?

touch.locationInNode(self) 

이것은 주어진 노드의 좌표계에서 (자기) 터치를 반환합니다. 따라서 캐릭터의 앵커 포인트가 노드의 중간 인 경우 (0.5, 0.5), 오른쪽에 터치 한 경우 location.x가 양수가되고 왼쪽에 터치 한 경우 negative가됩니다.

캐릭터의 x & y 위치와 비교하면 장면의 앵커 포인트에 따라 차이가 있지만 두 개의 다른 좌표계에서 x 값을 비교하려고하면 문제가 발생합니다.

0

당신의 self.magician은 자신의 포인트를 변환 할 수있는 다른 노드 (안 self)에 추가하는 경우 :

for touch in touches { 
      let location = touch.locationInNode(self) 

      print(location.x) 
      print("Pos \(self.magician.position.x)") 
      if location.x >= convertPoint(self.magician.position.x, fromNode: self.<thenodewhoaddmagician>) { 
       print("R") 
       self.movingRight = true 
      } else { 
       print("L") 
       self.movingLeft = true 
      } 
     } 

당신이 공식 애플 가이드 here

이 방법 모양에 대한 자세한 내용을 알고 싶다면
관련 문제