2017-11-29 14 views
1

SKNode A가 SpriteKit :

접촉이 SKNode B의 내부 시작하면 SKNode B.

의 부모 외부 노드의 시작 노드에서 접촉을 감지하는 방법 touchesMoved(_ touches: Set<UITouch>, with event: UIEvent?) 함수가 호출됩니다.

그러나 터치가 SKNode B 외부에서 시작되지만 SKNode B로 이동하면 touchesMoved 함수가 호출되지 않습니다.

하나의 옵션은 SKNode A로부터 모든 것을 처리 반대로 우리가 각 자녀에 터치 기능을 밀어 수, 이상적으로 SKNode A. 내부에 포함 된 많은 노드가,

그러나 SKNode의 A. 내부의 모든 터치 이벤트를 처리하는 것입니다

SKNode B 외부에서 시작된 경우에도 SKNode B 내부에서 발생하는 터치 이벤트를 캡처 할 수 있습니까?

+0

NodeB 내부의 접촉을 무시하고 있습니까? – ThrowingSpoon

+0

@ThrowingSpoon 예 – Crashalot

답변

2

touchesBegan 메서드를 누르면 터치가 종료 될 때까지 해당 노드의 좌표계로 터치가 제한됩니다. 다른 노드로 이동해도 좌표 시스템이 새 노드로 변경되지 않습니다. B가 터치가 필요한 유일한 노드이면 B를 발사하도록 전체 장면을 커버하는 하위를 추가 할 수 있습니다. 당신의 노드 B 클래스의 다음

class TouchNode : SKSpriteNode 
{ 
    //if you need to override other inits do it here 
    convenience init(withSize size:CGSize) 
    { 
     self.init(color:UIColor(red:0,green:0,blue:0,alpha:0.1),size: size) 
     isUserInteractionEnabled = true 

    } 

    override func touchesBegan(touches: Set<UITouches>, withEvent event: UIEvent?) { 
     parent!.touchesBegan(touches:touches, withEvent:event) 
    } 

    override func touchesMoved(touches: Set<UITouches>, withEvent event: UIEvent?) { 
     parent!.touchesMoved(touches:touches, withEvent:event) 
    } 

    override func touchesEnded(touches: Set<UITouches>, withEvent event: UIEvent?) { 
     parent!.touchesEnded(touches:touches, withEvent:event) 
    } 

    override func touchesCancelled(touches: Set<UITouches>, withEvent event: UIEvent?) { 
     parent!.touchesCancelled(touches:touches, withEvent:event) 
    } 

} 

:

required init(coder aDecoder:NSCoder) 
{ 
    super.init(coder:aDecoder) 
    DispatchQueue.main.async{ 
     self.addChild(TouchNode(withSize:self.scene!.size)) 
    } 
} 
override func touchesBegan(touches: Set<UITouches>, withEvent event: UIEvent?) { 
    //do stuff 
} 

override func touchesMoved(touches: Set<UITouches>, withEvent event: UIEvent?) { 
    //do stuff 
} 

override func touchesEnded(touches: Set<UITouches>, withEvent event: UIEvent?) { 
    //do stuff 
} 

override func touchesCancelled(touches: Set<UITouches>, withEvent event: UIEvent?) { 
    //do stuff 
} 

편집 : 오해 질문,하지만 경우 다른 사람의 대답을 떠나 외부에 노드 내부에서 이동해야합니다.

nodeB 외부에서 nodeB를 터치하고 싶다고 생각하면 이해가되지 않습니다. 그것은 내가 공기를 찔러 넣는 것과 같고 내가 당신을 파고하고 있다고 울고 있습니다. 당신이 절대적으로 경계의 외부 이동해야하지만, 노드 자체를 넘어 확장 당신이 그것을 터치 스프라이트에 자식 노드를 추가하고, 부모

class TouchNode : SKSpriteNode 
{ 
    //if you need to override other inits do it here 
    convenience init(withSize size:CGSize) 
    { 
     self.init(color:UIColor(red:0,green:0,blue:0,alpha:0.1),size: size) 
     isUserInteractionEnabled = true 

    } 

    override func touchesBegan(touches: Set<UITouches>, withEvent event: UIEvent?) { 
     parent!.touchesBegan(touches:touches, withEvent:event) 
    } 

    override func touchesMoved(touches: Set<UITouches>, withEvent event: UIEvent?) { 
     parent!.touchesMoved(touches:touches, withEvent:event) 
    } 

    override func touchesEnded(touches: Set<UITouches>, withEvent event: UIEvent?) { 
     parent!.touchesEnded(touches:touches, withEvent:event) 
    } 

    override func touchesCancelled(touches: Set<UITouches>, withEvent event: UIEvent?) { 
     parent!.touchesCancelled(touches:touches, withEvent:event) 
    } 

} 

에 백업 터치를 전송해야 그런 다음 NodeB 클래스에서 :

lazy var touchNode = TouchNode(withSize:self.scene!.size) 

override func touchesBegan(touches: Set<UITouches>, withEvent event: UIEvent?) { 
    addChild(touchNode) 
    //do other stuff 
} 
override func touchesEnded(touches: Set<UITouches>, withEvent event: UIEvent?) { 
    touchNode.removeFromParent() 
    //do other stuff 
} 

override func touchesCancelled(touches: Set<UITouches>, withEvent event: UIEvent?) { 
    touchNode.removeFromParent() 
    //do other stuff 
} 
+0

고마워요! 명확히하기 위해, 나는 당신과 동의한다. 그러나 이것은 목표가 아니다. :) 목표는 노드 B의 내부에있는 이벤트 만 캡처하는 것입니다. 문제는 터치가 B 내부에 있으면 B가 시작되면 무시됩니다 (즉, C에서 시작한 다음 비). – Crashalot

+0

오, 물론 질문을 잘못 읽었습니다. 노드 B의 내부를 호출했습니다. 호출되지 않을 것입니다. touches를 가질 수 없습니다. touchesBegan없이 움직입니다. 이 답변의 반대편을 할 필요가 있습니다. touchesmoved에서 자녀가 손대지 않았는지 확인하고 정보를 전달하십시오. – Knight0fDragon

+0

확인해야하지만, 노드 내부에서 시작하여 touchesMoved가 아니라면 꽤 확신합니다. 바깥 쪽에서 호출 될 때, 접촉 된 노드의 좌표계로 제한됩니다. – Knight0fDragon