manage game stages in cocos2d
i have a class that have a methods that create all my sprites,bodies,enemies,backgrounds of a stage in my game . the init call all this methods and create them on start.
now, i want to add many more stages,and do so in the right way .
i开发者_如何转开发 was thinking of creating a new class called : game-handler, so i have 2 classes the one i mention earlier will add the sprites and bodies, and when i need to go the the next level, i will call the game handler with the current stage, and get back strings of the new stage's images,and coordinates .
like that each stage class1 call the handler,get the right values to load,and add the sprites and bodies according to that values. is that correct ? the memory management will be ok like that ?
thanks a lot .
The way I do it is by making my custom CCScene subclass take some configuration values on init. Then I create another class that acts as a manager that will instantiate this CCScene for each new stage and pass it to CCDirector's replaceScene
method. That way, each stage is a fresh instantiation of the CCScene class so there will be no risk of unreleased memory or reused variable values.
A suggestion on how the manager class looks like:
@protocol GameManager : NSObject {
int currentLevel;
NSMutableArray *stageConfigs;
}
- (id)init;
+ (GameManager *)sharedManager;
- (void)goToLevel:(int)levelNum;
- (void)goToNextLevel;
@end
@implementation GameManager
...
- (void)goToLevel:(int)levelNum {
StageConfig *config = (StageConfig *)[stageConfigs objectAtIndex:levelNum];
GameScene *scene = [[GameScene alloc] initWithConfig:config];
[[CCDirector sharedDirector] replaceScene:scene];
[scene release];
}
...
@end
精彩评论