Subclass inheritance

Hi,


I am practising subclassing. I have a superclass GameScene and subclass Scene02. I created a scoring that is called in touches began. But when the app transitions to Scene02, the score returns to zero rather than inheriting the score update that is updated in the superclass GameScene init(). How can I make the score in Scene02 class to continue the last score (int 5) from the superclass?


Thanks in advance



import SpriteKit

import GameplayKit


class GameScene: SKScene {

var score = 0

let scoreLabel = SKLabelNode(fontNamed: "Arial")

required init?(coder aDecoder: NSCoder) {

super.init(coder: aDecoder)

}

override init(size: CGSize) {

super.init(size: size)

scoreLabel.fontSize = 35

scoreLabel.text = "Score: Score"

scoreLabel.fontColor = SKColor.black

scoreLabel.position = CGPoint(x: scene!.size.width/2.0, y: scene!.size.height/2.0)

scoreLabel.zPosition = 5

self.addChild(scoreLabel)

updateScore()

}

override func didMove(to view: SKView) {

self.anchorPoint = .zero

self.backgroundColor = SKColor.green

}

func updateScore() {

scoreLabel.text = "score: \(score)"

}

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

score += 1

updateScore()

if score == 5 {

let transition = SKTransition.fade(withDuration: 2.0)

let scene02 = Scene02(size: size)

view?.presentScene(scene02, transition: transition)

}

}

override func update(_ currentTime: TimeInterval) {

scoreLabel.text = "score: \(score)"

}

}




import SpriteKit

import GameplayKit


class Scene02: GameScene {


override func didMove(to view: SKView) {

self.anchorPoint = .zero

self.backgroundColor = SKColor.red

updateScore()

}

}

Accepted Reply

How can I make the score in Scene02 class to continue the last score (int 5) from the superclass?


Subclassing and passing values are completely differect things. Even if you create another instance of exactly the same class GameScene, the value of a proprty is not inherited.


You need to pass the last score to Scene02 explicitly.

For example:

    override func touchesBegan(_ touches: Set, with event: UIEvent?) {
        
        score += 1
        updateScore()
        
        if score == 5 {
            
            let transition = SKTransition.fade(withDuration: 2.0)
            let scene02 = Scene02(size: size)
            scene02.score = self.score //<-
            view?.presentScene(scene02, transition: transition)
        }
    }

Replies

How can I make the score in Scene02 class to continue the last score (int 5) from the superclass?


Subclassing and passing values are completely differect things. Even if you create another instance of exactly the same class GameScene, the value of a proprty is not inherited.


You need to pass the last score to Scene02 explicitly.

For example:

    override func touchesBegan(_ touches: Set, with event: UIEvent?) {
        
        score += 1
        updateScore()
        
        if score == 5 {
            
            let transition = SKTransition.fade(withDuration: 2.0)
            let scene02 = Scene02(size: size)
            scene02.score = self.score //<-
            view?.presentScene(scene02, transition: transition)
        }
    }

That’s awesome OOPer, thank you. So simple.


I am trying studying different design patterns and trying to achieve the same thing using a protocol. I’m just stabbing in the dark this time. Below is my clumsy attempt and of course Xcode is yelling. How can I make this work? Thanks in advance again.



import SpriteKit

import GameplayKit


protocol KeepScore {

var score: Int {get set}

var scoreLabel: String {get set}

}



extension KeepScore {

mutating func updateScore(count: Int) {

score += 1

let scoreLabel = SKLabelNode(fontNamed: "Arial")

scoreLabel.text = "score: \(score)"

}

func currentScore() -> Int { return score }

}



class GameScene002: SKScene, KeepScore {

var scoreLabel = "score: Score"

var score = 0

override func didMove(to view: SKView) {

self.anchorPoint = .zero

self.backgroundColor = SKColor.green

let scoreLabel = SKLabelNode(fontNamed: "Arial")

scoreLabel.fontSize = 35

scoreLabel.text = "Score: Score"

scoreLabel.fontColor = SKColor.black

scoreLabel.position = CGPoint(x: scene!.size.width/2.0, y: scene!.size.height/2.0)

scoreLabel.zPosition = 5

self.addChild(scoreLabel)

}


func updateScore() {

score += 1

let scoreLabel = SKLabelNode(fontNamed: "Arial")

scoreLabel.text = "score: \(score)"

}

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

updateScore()

if score == 5 {

let transition = SKTransition.fade(withDuration: 2.0)

let scene02 = Scene02(size: size)

view?.presentScene(scene02, transition: transition)

}

}

override func update(_ currentTime: TimeInterval) {

updateScore()

}

}


import SpriteKit

import GameplayKit


class Scene002: SKScene, KeepScore {


var scoreLabel: String = "Score: Score"

var score = 0

var currentScore = 0


override func didMove(to view: SKView) {

self.anchorPoint = .zero

self.backgroundColor = SKColor.red

currentScore()

}

}

Please try to format your code using the button `< >`.


And I do not understand what you are trying to do with protocols, you cannot pass between instances, even when the two are of the same class, or when one is an instance of the subclass and another of superclass. Using protocol or not.


If you want to pass some value from an instance to another instance, you need to pass it explicitly.

Ok Thank you. Appreciate your help.