Creating levels in an iOS Sprite Kit Game

那年仲夏 提交于 2019-12-06 01:23:37

问题


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.


回答1:


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.



来源:https://stackoverflow.com/questions/23312465/creating-levels-in-an-ios-sprite-kit-game

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