2015-12-23 93 views
1

我在檢測正在觸摸的特定節點時遇到問題。這是我所需要的。SKSpriteNode touch detected swift

let playagain = SKSpriteNode(imageNamed: "PlayAgain.png") 

override func didMoveToView(view: SKView) { 
    super.didMoveToView(view) 

} 

然後當玩家去世後,這兩個節點就出現了。

playagain.position = CGPoint(x:frame.size.width * 0.5, y: frame.size.height * 0.5) 
    addChild(playagain) 

    gameover.position = CGPoint(x:frame.size.width * 0.5, y: frame.size.height * 0.75) 
    addChild(gameover) 

以上所有的工作。節點出現在屏幕上,我問我似乎無法讓它顯示我點擊它。 ,你可以看到該節點被稱爲playagain,當點擊playagain節點時,我希望能夠刷新遊戲。到目前爲止,我的下面是。

override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) { 

    for touch in touches { 
     let location = (touch as! UITouch).locationInNode(self) 
     let play = self.nodeAtPoint(location) 
     if play.name == "playagain" { 

      println("touched") 
     } 
    } 
} 

謝謝!

回答

1

你不使用最新的Xcode嗎?你倒是開始代碼不應與SWIFT 2運行和Xcode的7

試試這個

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    for touch in touches { 
     let location = touch.locationInNode(self) 

     if playagain.containsPoint(location) { 

     /// playagain was pressed, do something 
     } 
    } 
} 

,或者由於工作這

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
    for touch in touches { 
     let location = touch.locationInNode(self) 
     let touchedNode = self.nodeAtPoint(location) 

     if touchedNode == playagain { 

     /// playagain was pressed, do something 
     } 
    } 
} 
+0

啊! –

+0

您能否請您將問題標記爲已回答。謝謝 – crashoverride777