-1

I am trying to implement a pause button in my game. However every time I tap the pause button (SKSpriteNode) on my iOS Device nothing happens. I have tried making the button do other actions and tried making other sprites do the action. None have worked, although I am able to touch any location on the screen and the action is performed. I am using Swift 4 with the latest version of Xcode (9.4.1). The app is a iOS Game and I am using the GameScene.swift that is created along with the app.

Here is the part of the code for the button (irrelevant parts of the code are left out):

import SpriteKit
import CoreMotion
import GameplayKit

class GameScene: SKScene, SKPhysicsContactDelegate {

var pauseButton:SKSpriteNode!

override func didMove(to view: SKView) {

    pauseButton = SKSpriteNode(imageNamed: "pauseButton")
    pauseButton.size = CGSize(width: 50, height: 50)
    pauseButton.position = CGPoint(x: self.size.width - 
    pauseButton.size.width, y: self.size.height - 
        pauseButton.size.height)
    pauseButton.zPosition = 5
    self.addChild(pauseButton)

}

override func touchesEnded(_ touches: Set<UITouch>, with: UIEvent?) {

    fireBullet() //This function does not relate to the pause button.

}

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

    let touch = touches.first

        if let location = touch?.location(in: self) {
            let nodesArray = self.nodes(at: location)

        if nodesArray.first?.name == "pauseButton" {
            self.view?.isPaused = true
        }
    }
}
}

Thanks in advance for taking your time to reply it really helps me! Thomas

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Thomas
  • 37
  • 1
  • 1
  • 6

1 Answers1

0

Easy fix, in your touchesBegan method you are searching for touches on a node named "pauseButton" but there are no nodes named "pauseButton" so your search returns a false value.

Just add pauseButton.name = "pauseButton" to your code where you set up the pause button and it should work.

E. Huckabee
  • 1,788
  • 1
  • 13
  • 29