2014-10-25 4 views
0
신속

에 SKSprite 노드를 클릭하고 나는 그것이 SKSpriteNode에 클릭 이벤트를 추가하는 방법에 대해 내가 가서 어떻게 지금은 거의 이틀 :(정말 정말 좌절하고

을 위해 일하려고 노력하고있다 빠른

let InstantReplay:SKSpriteNode = SKSpriteNode(imageNamed: "InstantReplay") 
    InstantReplay.position = CGPoint(x: size.width + InstantReplay.size.width/2, y: size.height/2) 
    InstantReplay.size = CGSize(width: size.width/1.4, height: size.height/8) 
    addChild(InstantReplay) 

    InstantReplay.runAction(SKAction.moveToX(size.width/2, duration: NSTimeInterval(1))) 

에 나는 일이 원하는 건

어떤 도움이 많이 감사? 나는이 구현 가겠어요 방법은 "InstantReplay_Clicked"라는 함수를 실행하려면 "InstantReplay는"클릭 할 때입니다 :)

답변

3

SKSpriteNode에 이름을 지정하여 SKSene의 touchesBegan 또는 touchesEnded 메소드에서 식별 할 수 있도록하십시오.

또한 SKSpriteNode의 userInteractionEnabled를 false로 설정하여 터치 이벤트를 스스로 트래핑하지 않고 장면까지 전달하지 않도록합니다.

override init() { 
    super.init() 

    let instantReplay = SKSpriteNode(imageNamed: "InstantReplay") 
    instantReplay.position = CGPoint(x: size.width + instantReplay.size.width/2, y: size.height/2) 
    instantReplay.size = CGSize(width: size.width/1.4, height: size.height/8) 
    instantReplay.name = "InstantReplay"; // set the name for your sprite 
    instantReplay.userInteractionEnabled = false; // userInteractionEnabled should be disabled 
    self.addChild(instantReplay) 
} 

override func touchesBegan(touches: NSSet, withEvent event: UIEvent) { 
    let location = touches.anyObject()?.locationInNode(self) 
    let node = self.nodeAtPoint(location!) 
    if (node.name == "InstantReplay") { 
     println("you hit me with your best shot!") 
    } 
} 

는 (오 - 나는 또한 Swift best practices에 맞춰, lowerCamelCase를 사용하도록 instantReplay 변수의 이름을 변경

.