user3835611
user3835611

Reputation: 129

How to refrence another variable from different clases in Swift

So im trying to refrence a variable called high score in a diffrent file in a class called PlayScene. How do i refrence that high score in my GameScene.swift file. Here is my GameScene.swift file currently but at the line with

self.highscoreText.text = "Highscore: \(highscoreshow)

it says that highscoreshow is an unresolved identifier because I don't know how to refrence that variable from another class

import SpriteKit

class GameScene: SKScene {

    let playButton = SKSpriteNode(imageNamed:"play")
    let title = SKSpriteNode(imageNamed: "title")
    let highscoreText = SKLabelNode(fontNamed: "Chalkduster")

    override func didMoveToView(view: SKView) {
        self.playButton.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame))
        self.title.position = CGPointMake(CGRectGetMaxX(self.frame) - 150
            , CGRectGetMaxY(self.frame) - 150)
        self.highscoreText.text = "Highscore: \(highscore)"
        self.highscoreText.fontSize = 42
        self.highscoreText.position = CGPointMake(CGRectGetMidX(self.frame), CGRectGetMidY(self.frame) - 150)
        self.addChild(self.playButton)
        self.addChild(self.title)
        self.addChild(self.highscoreText)
        self.backgroundColor = UIColor(hex: 0x80D9FF)
    }

    override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
        for touch: AnyObject in touches {
            let location = touch.locationInNode(self)
            if self.nodeAtPoint(location) == self.playButton {
                var scene = PlayScene(size: self.size)
                let skView = self.view as SKView
                skView.ignoresSiblingOrder = true
                scene.scaleMode = .ResizeFill
                scene.size = skView.bounds.size
                skView.presentScene(scene)
            }
        }
    }

    override func update(currentTime: CFTimeInterval) {
        /* Called before each frame is rendered */
    }
}

Here is the function in the other class in which the highscore variable is created and stored:

func died() {

    var defaults = NSUserDefaults()

    var highscore = defaults.integerForKey("highscore")

    if (score > highscore){

        defaults.setInteger(score, forKey: "highscore")

    }

    var highscoreshow = defaults.integerForKey("highscore")

    if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
        let skView = self.view as SKView
        skView.ignoresSiblingOrder = true
        scene.size = skView.bounds.size
        scene.scaleMode = .AspectFill
        skView.presentScene(scene)
    }
}

Upvotes: 1

Views: 807

Answers (1)

Luca Angeletti
Luca Angeletti

Reputation: 59506

Ok, there are 2 problems.

  1. It is possible that the variable highscoreshow does not exist when you try to access it.
  2. The variable highscoreshow can be accessed only within the method PlayScene.died (where it is declared).

You could use NSUserDefaults to share the value highscoreshow between 2 Scenes.

  1. Just add this line in GameScene of top of the method didMoveToView

    var highscoreshow = NSUserDefaults().integerForKey("highscore")

  2. Remember to update the value in NSUserDefaults if you change highscore

    NSUserDefaults().setInteger(highscoreshow, forKey: "highscore")

  3. Finally please note that values NSUserDefaults are save on persistent storage. So these values will still be available if you restart the app or the device.

Hope this will help.

Upvotes: 1

Related Questions