Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating levels in an iOS Sprite Kit Game

I'm creating a game and I want it to have a scene with levels to select, then have the levels to play. I was just wondering how to go about creating the levels and saving when a user has gotten to that level. I was thinking to create a BOOL in the ViewController.h and call it in each scene, i.e. LevelCompleted = YES; then use that bool when the player is trying to play a new level or replay a previous level

Or,

Do I have a BOOL in each scene, then import each scene's header file into the level-navigating scene, and use a BOOL to show if the level is unlocked or not.

Also, to save the BOOL's value when the app is closed, do I use NSUserDefaults?

I've searched a lot for tutorials on this and can't seem to find any. If more info is needed please let me know.

like image 296
user3576196 Avatar asked Apr 26 '14 14:04

user3576196


People also ask

Does SpriteKit use GPU?

SpriteKit runs efficiently on the GPU. CoreGraphics runs on the CPU.

Is SpriteKit easy?

SpriteKit is easy to learn because it is a well-designed framework and it is even easier if you have experience with Swift. Even for a beginner, if you want to create your first game, 2D games is without a doubt the best way to transit in this new world.

What is the difference between SceneKit and SpriteKit?

SceneKit and SpriteKit are very similar to each other. SceneKit is a little harder to learn but it's pretty simple. SceneKit would be the only way to have a 3D model(With the options you provided). You can have a SpriteKit scene over top of the SceneKit scene to display labels that stay put.

Is SpriteKit any good?

Welcome to game development! It rocks and I love it. If you're familiar with iOS development or Swift, then SpriteKit is a great place to dive in and dabble. You can learn the basics of game physics, game loops, event handling, and everything else you'll need to know playing with this framework.


1 Answers

There are a couple of ways to do what you want. I will give you some tips and hints so you can create the code which fits your game.

First remember that MyScene is a SKScene which is called by default from the View Controller in a SpriteKit project template. So you can create as many SKScene classes as you want to have levels. Each SKScene class can be one level.

You can turn your first SKScene, MyScene, into your main menu and add the list of levels for the user to select here. To get to a selected level from your 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];

Level1 would 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 include a scaleMode property depending on your needs.

As LearnCocos2D already pointed out, you can use either an array or dictionary to store your player's data such as items, health and levels achieved. Unless you have a large amount of data you need to save, NSUserDefaults is your best option. Here is an example on how to store data with NSUserDefaults:

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

To read a stored value in NSUserDefaults you can do this:

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

You can write new data to NSUserDefaults at any time. So if the player just finished a level, write it to NSUserDefaults.

like image 59
sangony Avatar answered Oct 04 '22 20:10

sangony