user2331875
user2331875

Reputation: 134

Hold a level number in SpriteKit

I would like to reload my scene, after some event in the game. But I would like to keep parameter for level (to increase it). How would you do that? I am a beginner in iOS development. I am having an idea of having some global variable, but not sure if that is possible and that is the right way to go. Thanks!

Upvotes: 1

Views: 289

Answers (2)

Bruno Biesemans
Bruno Biesemans

Reputation: 23

You can use a property-list file for this.

Upvotes: 0

AndyOS
AndyOS

Reputation: 3606

There are several choices,a few are:

  • a property on the scene's parent view controller that you can update from your scene
  • a singleton class can be used (I wouldn't advise this though, can cause issues)
  • core data

If you want to keep the level value for future use (e.g. if the player can load their game at a later date), I'd use core data. If you don't need to store it long-term, I'd use a property on the parent view controller:

YourViewController.h:

#import YourScene.h

...

@property int level;

YourViewController.m

...
//before you present the scene
yourScene.viewController = self;
...

YourScene.h

#import YourViewController.h

@property (nonatomic,weak) YourViewController *viewController;

YourScene.m

...
//store the level in the parent controller
_viewController.level = 1;
...

Upvotes: 1

Related Questions