Luiz
Luiz

Reputation: 1315

Pause/Play freezing game

I'm trying to make a pause button and a play button for my game, but I don't know what happens that the screen just freezes when I touch pause button (play button appears and remove pause button) and then touch play button (freeze).

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()
    }
}

Upvotes: 0

Views: 151

Answers (1)

crashoverride777
crashoverride777

Reputation: 10664

It makes no sense to create the button when you touch the screen. That means you are creating a new button every time you touch the screen. Move all the code above "//when touch button" out of the touches method and put it in didMoveToView

Your code structure could look something like this

 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()
              }
          }
      }
 }

Alternatively you could also add all your buttons to the scene and than for example hide the pauseButton. Than instead of removing and adding buttons you just hide and unhide them

 pauseButton.hidden = true

This only works correctly if your target is ios 9 or above where hidden nodes no longer receive touch events.

Hope this helps

Upvotes: 3

Related Questions