Jonathan
Jonathan

Reputation: 2383

Levels - Cocos2d

I have 10 Levels. Just like a normal game, you can't play the next level unless you beat the level before it. The only parts that change on each level is, one sprite and the sprite's position.

How could I make it so if you beat the level the next level unlocks, then distinguishes which level you are on and loads the defined "details"(meaning sprite file and position)?

Any help is great appreciated! Thanks!

EDIT:

I used a plist to load all the information regarding the level

First, I created the PLIST(right click a group then, click resources, "Property List"). Next, I defined all information within the PLIST.

Then, I set up a global variable to set the level number. When the user chooses whichever level(which is a CCMenu), the method changes the int "level" to the correct level number(e.g. 1,2,3,4,5). Then, in my game init it checks the int "levels" and runs the level from the PLIST.

This is in my level methods that the gamescene init runs after the level check. It loads the PLIST, sets the dictionary, chooses the background, adds it to the layer, then the ints "_ballX, _ballY" are declared in the .h file which sets the value for the ball's position located in the init!

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"Levels.plist"];
NSDictionary *plistDataAll = [[NSDictionary dictionaryWithContentsOfFile:finalPath] retain];
NSDictionary *levelData = [NSDictionary dictionaryWithDictionary:[plistDataAll valueForKey:@"LevelOne"]];

NSString *background = [levelData objectForKey:@"LevelBackground"];

_m = [CCSprite spriteWithFile:background];
_m.anchorPoint = ccp(0, 0);
_m.position = ccp(0, 0);

[self addChild:_m];

_ballX = [[levelData valueForKey:@"BallPositionX"] intValue];
_ballY = [[levelData valueForKey:@"BallPositionY"] intValue];

Hope this helps someone! Thank you to everyone for the very quick responses!

Upvotes: 3

Views: 1312

Answers (2)

jhocking
jhocking

Reputation: 5577

Have your level class load the data for the level from some data file (a plist file, or xml, or whatever). Then whenever you want to load a different level just point to a different data file.

As for unlocking levels, simply make an array of levels along with their locked/unlocked status that the level select class uses to display the different levels. Between games that information can be stored in another data file (again, a plist file, or xml, or even using CoreData).

Upvotes: 2

Oscar Gomez
Oscar Gomez

Reputation: 18488

The way I handled it is basically have an abstract class, and implement this abstract class in a separate class for each level, then whenever that level is done, simply load the next class (level).

Upvotes: 3

Related Questions