Search code examples
swiftstaticsprite-kitinversion-of-controlskscene

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


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?


Solution

  • 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