The best way to implement lives and score count in Sprite Kit (static, IoC, other?)

可紊 提交于 2019-12-13 03:16:37

问题


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?


回答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




回答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



来源:https://stackoverflow.com/questions/26448678/the-best-way-to-implement-lives-and-score-count-in-sprite-kit-static-ioc-othe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!