2015-01-14 2 views
0

내 사용자 정의 클래스가 아닌 GameScene에서 터치를 감지하고 싶습니다. 내 클래스에서 userInteractionEnabled = true로 설정하면 쉽게 감지 할 수 있습니다. 터치 된 노드). 그러나 GameScene에서 나는 touch를 감지 할 필요가 있기 때문에 하나 이상의 custom 클래스가 있습니다. 그러나터치 노드를 SKNode가 아닌 SKSpriteNode에서 파생 된 사용자 정의 클래스

let location = touch.locationInNode(self) 
let touchedNode = nodeAtPoint(location) 

여기서 nodeAtPoint는 SKNode를 반환하지만 내 작업을 수행하려면 사용자 정의 클래스가 필요합니다. 바인딩 옵션과 함께 도움

답변

3

사용하십시오 condtional 캐스트 (as?)에 대한 감사 (if let) :

let location = touch.locationInNode(self) 
if let touchedNode = nodeAtPoint(location) as? MyCustomClass { 
    // If we get here, touchedNode is type MyCustomClass 
} 

당신이 다른 방법으로 처리해야 여러 클래스가있는 경우이 스위치를 사용할 수 있습니다 성명 :

switch nodeAtPoint(location) { 
    case let node as MyCustomClass1: 
     // handle node of type MyCustomClass1 
    case let node as MyCustomClass2: 
     // handle node of type MyCustomClass2 
    case let node as MyCustomClass3: 
     // handle node of type MyCustomClass3 
    default: 
     // Nothing to do here 
     break 
} 
+0

감사합니다. –

+0

@phrts 추가적으로 캐스팅 변수에 대한 정보를 아직 보지 않았다면 더 자세히 살펴볼 수도 있습니다. – Scott

관련 문제