danieldevtech danieldevtech - 7 months ago 17
Swift Question

SpriteKit: var from GameScene.swift TO GameViewController.swift?

I've created a complete game in SpriteKit's GameScene.swift - In here I have the scoring system which is a simple var called var currentScore = 1.

I want to incorporate Game Center within my game too. I'm doing this by adding a button on my Main.storyboard but the score that I want to send to Game Center is within the GameScene.swift (the var currentScore = 1)

Here's the function for the scoring inside GameScene.swift:

func nextLevel(){
currentLevel += 1
currentScore = currentLevel
levelLabel.text = "\(currentScore)"
showLevel.text = "\(currentScore)"
won()
if currentLevel > highLevel{
highLevel = currentLevel
let Defaults = NSUserDefaults.standardUserDefaults()
Defaults.setInteger(highLevel, forKey: "highLevel")
}


How do I add this currentScore and/or currentLevel variable inside my GameViewController.swift? Thank you.

Answer

You can declare currentScore and currentLevel as global variables. For example,

var currentScore = 0
var currentLevel = 0
class GameScene ... {
}

You can access these variables in your GameViewController class because they are "Global Variables".