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