1

I have two scenes: my GameScene where the player tries to beats his score and GameOverScene which I present to the player when he loses:

func changeScene() {
    let scene = GameOverScene()
    view.presentScene(scene)
}

Basically I want to pass the player score to my GameOverScene so I can show it to the player. The score is stored in the property: "score: Int" inside my GameScene, but I'm not sure how to pass it between scenes. How can I do it?

lisovaccaro
  • 32,502
  • 98
  • 258
  • 410
  • Despite of the code, the concepts are here: http://stackoverflow.com/questions/11577484/passing-variables-between-view-controllers – Fer Aug 29 '14 at 23:45

1 Answers1

0

You can set the new scene's PROPERTIES with the following function:

override func prepareForSegue(segue: UIStoryboardSegue!, sender: AnyObject!) {
    if (segue.identifier == "Load View") {
        // pass data to next view
    }
}

Here is a detailed example:

http://jamesleist.com/ios-swift-passing-data-between-viewcontrollers/

Fer
  • 460
  • 2
  • 4
  • 17
  • Hi Fer, I have created different scenes but I only have one viewController in my app: "GameViewController" and three scenes "StartScreen" and the two mentioned in the question. From what I'm reading I understand this works to pass data between viewControllers, does that mean I need to add more viewControllers to the game? – lisovaccaro Aug 30 '14 at 00:01
  • @lisovaccaro Hey man! have you tried using the dictionary provided with your new scene? try: myNewScene.userData setObject just like any NSMutableDictionary – Fer Aug 30 '14 at 00:09
  • I had just implemented that. I'll edit your answer with my code. Your answer made me wonder about how many View Controllers should I have, if you know about it please check this question I just asked: http://stackoverflow.com/questions/25577743/how-many-view-controllers-should-i-have-in-my-game – lisovaccaro Aug 30 '14 at 00:15