Swift: updated variable in another scene

I have a variable in my GameScene, it takes the variable into account, and it is displayed on the screen using SKLabelNode.

Each time a collision occurs, 1 is added to the standings, which is passed as a string to SKLabelNode, and then updated on the screen.

The problem is that when I call it from my GameOverScene (the scene where the final result is displayed, along with the "game"), I get the first score value equal to 0. As in GameOverScene, the value of the variable, but not updated. How to get an updated variable? Can anyone help?

The code:

class GameScene: SKScene, SKPhysicsContactDelegate {
    // S C O R E !
    var score = 0
    var scoreText: String = ""

    var scoreOnScreen = SKLabelNode()
...

override func didMoveToView(view: SKView) {
    // S C O R E
    scoreOnScreen.position = CGPoint(x: size.width / 2, y: size.height * 0.8)
    scoreText = String(score)
    scoreOnScreen.text = scoreText

when a collision occurs, the rating gets +1

func bulletDidCollideWithEnemy(enemy: SKSpriteNode, bullet: SKSpriteNode) {

        score++
        scoreText = String(score)
        scoreOnScreen.text = scoreText

and then GameOverScene:

import SpriteKit
import UIKit

class GameOverScene: SKScene {

    let GameSceneInstance = GameScene()
    let bgImage = SKSpriteNode(imageNamed: "background")
    let afraidLogo = SKSpriteNode(imageNamed: "gameoverlogo")
    var gameOverLabel = SKLabelNode()

    override func didMoveToView(view: SKView) {

        GameSceneInstance.scoreOnScreen.text = String(GameSceneInstance.score)

        bgImage.position = CGPoint(x: size.width / 2, y: size.height / 2)
        bgImage.setScale(0.75)
        addChild(bgImage)

        afraidLogo.position = CGPoint(x: size.width / 2, y: size.height / 2)
        afraidLogo.setScale(0.50)
        addChild(afraidLogo)

        gameOverLabel.fontSize = 40
        gameOverLabel.fontColor = SKColor.whiteColor()
        gameOverLabel.text = "score: \(GameSceneInstance.scoreOnScreen.text)"
        gameOverLabel.position = CGPointMake(self.size.width/2, 2.0 / 3.0 * self.size.height);
        addChild(gameOverLabel)

    }

I am sure this is just a code issue. Appreciate any help.

+4
1

reddit Jasamer. .

:

let scene = GameOverScene(size: skView.bounds.size)

:

    scene.gameScene = self
    scene.score = score

GameOverScene:

class GameOverScene: SKScene {

    var gameScene = GameScene()
override func didMoveToView(view: SKView) {
   gameOverLabel.text = "score: \(gameScene.score)"

.

, , scene.GameScene = self.

, - .

+4

All Articles