Alex
Alex

Reputation: 1322

Organizing files in Cocos2d

I have just started learning Cocos2d using the great tutorials Ray Wenderlich has produced. However I'm getting to a point where my HelloWorldScene main file is getting a little large.

I would like to be able to sort sprite generation methods into one file, misc. methods into another and so on, leaving me with the core level files containing the different scenes and init methods.

Is it possible to copy methods over to a new .m file and bring them into the HelloWorldScene.m when I need them?

How do you organize your game files?

Upvotes: 2

Views: 476

Answers (1)

Ospho
Ospho

Reputation: 2796

I highly recommend layering your game. It keeps it organised and neater, reducing the amount of code in any one file.

Within cocos2d, there are 3 classes you should take note of to achieve this:
CCDirector, CCScene, CCLayer.

What you can do is, create multiple layers within a single scene (GameScene).Eg: Have a HUD, Game Interface, Background and Foreground.

These can all be split into their separate CCLayers and added together within the CCScene.

So now you have split HelloWorld.m into: GameScene.m
HudLayer.m
GameInterfaceLayer.m
Background.m
Forground.m

After importing all the h files into your GameScene.h, You can then add each Layer to the scene as Follows:

@implementation GameScene
+(CCScene){

HudLayer *hud = [HudLayer node];
[self addChild: hud z:3];

GameInterfaceLayer *game = [GameInterfaceLayer node];
[self addChild: game z:1];
...} etc
@end

Once you do this, if you dont change your AppDelegate, your CCDirector will give you an error. CCDirector basically dictates which Scene is loaded.
Change this to point to your GameScene.

You can visualise: the Director of the Movie, Filming a Scene, filled with many Layers.

Upvotes: 5

Related Questions