2016-04-04 18 views
0

私はゲームの一時停止ボタンと再生ボタンを作ろうとしていますが、一時停止ボタンをタッチすると画面がちょうどフリーズすることはわかりませんボタン)をタッチし、再生ボタン(フリーズ)をタッチします。フリーズゲームを一時停止/再生する

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

    //Pause 
    pauseButton = SKSpriteNode (imageNamed: "pause") 
    pauseButton.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2) 

    self.addChild(pauseButton) 

    //Play 
    playButton = SKSpriteNode (imageNamed: "play") 
    playButton.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2) 


    //when touch buttons 
    let touch = touches.first! 
    if pauseButton.containsPoint(touch.locationInNode(self)) { 
     addChild(playButton) 
     pauseButton.removeFromParent() 
    } 
    if playButton.containsPoint(touch.locationInNode(self)) { 
     addChild(pauseButton) 
     playButton.removeFromParent() 
    } 
} 

答えて

2

画面に触れたときにボタンを作成することは意味がありません。つまり、画面に触れるたびに新しいボタンを作成しています。タッチ方式のうち、上記のすべてのコード「//タッチボタン」を移動し、didMoveToView

に入れ

あなたのコードの構造はまた、あなたも、すべてのボタンを追加することができ、この

class GameScene: SKScene { 

     var pauseButton: SKSpriteNode! // to make your code even safer you could use optionals here 
     var playButton: SKSpriteNode! // to make your code even safer you could use optionals here 

     override func didMoveToView(view: SKView) { 
     //Pause 
     pauseButton = SKSpriteNode (imageNamed: "pause") 
     pauseButton.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2) 

     self.addChild(pauseButton) 

     //Play 
     playButton = SKSpriteNode (imageNamed: "play") 
     playButton.position = CGPoint(x: self.frame.width/2, y: self.frame.height/2) 
    } 

    override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) { 
      for touch in touches { 

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

       //when touch buttons 
       if node == pauseButton { 
        addChild(playButton) 
        pauseButton.removeFromParent() 
       } 

       if node == playButton { 
        addChild(pauseButton) 
        playButton.removeFromParent() 
       } 
      } 
     } 
} 

のようになります例えば、pauseButtonを隠すよりも、シーンに追加する必要があります。代わりにボタンを削除し、追加のあなたがちょうどそれらを隠し、再表示よりも

pauseButton.hidden = true 

あなたの目標は、IOS 9または非表示のノードはもはやタッチイベントを受け取る場所を超えている場合にのみ正しく動作します。

希望します。

+0

大変申し訳ございません。 「タッチボタンを押すと」の後に「let touch = touches.first!」を使うのを忘れてしまった。私はdidMoveToViewに移動しようとしましたが、これは私にこのエラーを与えます: "未解決の識別子の使用 'をタッチします。私はコーディングに新しいので、私はそれをほとんど知らない。 =( – Luiz

+0

私は答えを更新しました – crashoverride777

+0

ありがとうございました!!それは働いていました!=) – Luiz

関連する問題