0

I have background in Java but havn't been coding in years. Lately I've taken interest to warm my coding skills again and have chosen to create learning apps for my kids in Swift. I've created basic Swift game utilizing the Sprite Kit with gameviewcontroller and multiple scenes. However I run into a basic question which is related to passing basic data such as points and lives counts from scenes to gameviewcontroller.

Back in the day, I would have done it by creating a static member that would hold the lives left and score count but how is it today in Swift? I undertand that IoC would be the more modern equilevant to static members but howabout Swift and howabout this case?

Are there IoC frameworks for Swift or StackOverFlow users' proposals for solution concept?

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
user3673836
  • 591
  • 1
  • 9
  • 23
  • Sorry if my question seems to be stupid but you want save data when you close your app (like highscore) or just pass data between several classes ? – Haox Oct 19 '14 at 08:22
  • Not a stupid question! Initially I only thought about passing of data between classes but saving highscores would be the next step. (I just haven't gotten to it yet :) Thanks! – user3673836 Oct 19 '14 at 08:34
  • To save your highscore it really simple. You just have to use set `default.setInteger()` like this : if you score variable is named `yourScore` so you code to save it if it is a highscore is : `default.setInteger(yourScore, forKey: "highscore")` don't forget that to be global you have to declare it out of classes like this : `var defaults = NSUserDefaults() ; var highscore = defaults.integerForKey("highscore")`. With this you can call your highscore everywhere and your score is saved in the device memory – Haox Oct 19 '14 at 08:49
  • Sorry to keep on asking about the globals but now I'm wondering how to introduce a global function for updating the labels...so i have scoreCountLabel in GameViewController and now thanks to you I'll get its data from global variable but how do I call GameViewController.updateLabels() from GameScene to update the scorecount on screen? – user3673836 Oct 19 '14 at 09:08
  • Make another post for this question it will be easier to answer with all text settings – Haox Oct 19 '14 at 09:23
  • Good point! Posted here: http://stackoverflow.com/questions/26449343/global-function-call-in-swift-howto Thanks! – user3673836 Oct 19 '14 at 09:57

2 Answers2

2

I would also use a separate GameState singleton class to manage all of that stuff, just to make sure there's no conflicts, and you can access the data from anywhere. Here's a really good tutorial for that, and it should be a cinch to update for swift: http://www.raywenderlich.com/63235/how-to-save-your-game-data-tutorial-part-1-of-2

1

To pass data between several classes your variable has to be global. You have to declare your variable out of classes.

For exemple :

var score:Int = 0

class GameScene {
   //Your game here
}

Like this your will call score in all others class like your GameViewController

Haox
  • 608
  • 1
  • 7
  • 23