Search code examples
iosswiftsprite-kitgameplay-kit

I constantly get the error 'Instance member isPaused cannot be used on type GameScene'


This is the code I have and I have a button that when I click it, I want to pause the game to show a menu... I have comments where the error is showing up, so you can hopefully see what I'm doing wrong.

Also I'm not sure if this code is supposed to be in my GameScene file, if not could you let me know which one it should be in.

Thanks for trying to help.

//
//  GameScene.swift
//  Pong2
//
//  Created by Hussain on 10/11/16.
//  Copyright © 2016 Archetapp. All rights reserved.
//

import SpriteKit
import GameplayKit

class GameScene: SKScene {

    var ball = SKSpriteNode()
    var enemy = SKSpriteNode()
    var main = SKSpriteNode()

    var topLbl = SKLabelNode()
    var btmLbl = SKLabelNode()

    var score = [Int]()

    @IBOutlet weak var pauseBetton: UIButton! // HERE IS MY OUTLET


    override func didMove(to view: SKView) {

        func pauseButton (sender: UIButton){ // HERE'S MY FUNCTION TO TRY AND PAUSE MY GAME
            let pauseAction = SKAction.run {
                GameScene.isPaused = true // HERE'S THE LINE I GET MY ERROR
                debugPrint("Paused")
            }
        }



        topLbl = self.childNode(withName: "topLabel") as! SKLabelNode
        btmLbl = self.childNode(withName: "btmLabel") as! SKLabelNode
        ball = self.childNode(withName: "ball") as! SKSpriteNode

        print(self.view?.bounds.height)

        enemy = self.childNode(withName: "enemy") as! SKSpriteNode
        enemy.position.y = (self.frame.height / 2) - 50

        main = self.childNode(withName: "main") as! SKSpriteNode
        main.position.y = (-self.frame.height / 2) + 50

        let border  = SKPhysicsBody(edgeLoopFrom: self.frame)

        border.friction = 0
        border.restitution = 1

        self.physicsBody = border

        startGame()
    }
}

Solution

  • isPaused is an instance method, not a class method. You need to called it on a specific instance of a SKNode such as your GameScene instance.

    Change GameScene.isPaused = true to self.isPaused = true or simply isPaused = true

    Besides that issue, you need to move your pauseButton method from inside your didMove(to:) method.

    func pauseButton (sender: UIButton) {
        let pauseAction = SKAction.run {
            self.isPaused = true
            debugPrint("Paused")
        }
    }
    
    override func didMove(to view: SKView) {
        topLbl = self.childNode(withName: "topLabel") as! SKLabelNode
        btmLbl = self.childNode(withName: "btmLabel") as! SKLabelNode
        ball = self.childNode(withName: "ball") as! SKSpriteNode
    
        print(self.view?.bounds.height)
    
        enemy = self.childNode(withName: "enemy") as! SKSpriteNode
        enemy.position.y = (self.frame.height / 2) - 50
    
        main = self.childNode(withName: "main") as! SKSpriteNode
        main.position.y = (-self.frame.height / 2) + 50
    
        let border  = SKPhysicsBody(edgeLoopFrom: self.frame)
    
        border.friction = 0
        border.restitution = 1
    
        self.physicsBody = border
    
        startGame()
    }