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