Create levels in iOS Sprite Kit Game

I am creating a game and I want it to have a scene with levels for selection, and then levels for playback. I'm just wondering how you can create levels and save when the user has reached this level. I thought of creating a BOOL in ViewController.h and calling it in each scene, that is, LevelCompleted = YES; then use this bool when a player tries to play a new level or play a previous level

Or

I have a BOOL in each scene, then import each scene title file into the scene with level navigation and use BOOL to show whether it is unlocked or not.

Also, to keep the BOOL value when the application is closed, do I use NSUserDefaults?

I searched a lot for textbooks on this subject and didn't seem to find it. If you need more information, please let me know.

+6
source share
1 answer

There are several ways to do what you want. I will give you some tips and advice so that you can create a code that matches your game.

First of all, remember that MyScene is SKScene, which is called by default from the View Viewer in the SpriteKit project template. This way you can create as many SKScene classes as you want. Each SKScene class can be at one level.

You can turn your first SKScene, MyScene into your main menu and add a list of levels for the user to select. To go to the selected level in the main menu (MyScene), you can use something like this:

SKScene *gameLevel1 = [[Level1 alloc] initWithSize:self.size]; SKTransition *reveal = [SKTransition doorsOpenVerticalWithDuration:1.0]; [self.view presentScene:gameLevel1 transition:reveal]; 

Level 1 will be the name of your SKScene class for your first level. There are also a number of great transitions to choose from. You can also enable the scaleMode property, depending on your needs.

As already stated in LearnCocos2D, you can use either an array or a dictionary to store player data, such as items, health and levels reached. NSUserDefaults is your best option if you do not have a lot of data that you need to save. Here is an example of how to store data using NSUserDefaults:

 NSString *valueToSave = @"Level 5"; [[NSUserDefaults standardUserDefaults] setObject:valueToSave forKey:@"levelReached"] [[NSUserDefaults standardUserDefaults]synchronize]; 

To read the stored value in NSUserDefaults, you can do this:

 NSString *highLevel = [[NSUserDefaults standardUserDefaults] stringForKey:@"levelReached"]; 

You can write new data to NSUserDefaults at any time. Therefore, if a player has just completed a level, write his NSUserDefaults.

+2
source

All Articles