Thursday, 10 January 2013

The structure of a Cocos2d project


A Cocos2d project has a buildup somewhat comparable to applications such as Adobe Photoshop. 
A Cocos2d game consists of scenes, which contain layers, which contain sprites, labels and other objects.


An example of this could be the main gameplay scene of a game. Let’s say it contains two layer: one showing the actual gameplay, and one on top of the gameplay layer showing the score (the Heads Up Display –HUD). The gameplay layer would contain the sprites of the player and the level, and the HUD layer would have a label showing the score. The game might have other scenes, such as a main menu scene.

Figure 1-1. Typical buildup of a Cocos2d game


        You can structure your game by adding children to an instance of CCScene, CCLayer or CCSprite (the Cocos2d classes for scenes, layers and sprites). This can be done by calling the following method:

[sprite addChild: sprite];

Optionally, one could send a z-argument when calling the method, to set the order in which the object is shown by its parent. For example, the HUD layer of a game should be on top of the gameplay layer, and should therefore be given a higher z value:

[scene addChild: hudLayer z:1];

            Scenes are typically changed between different gamestates, such as changing from the main menu to the level selection screen. Layers are mostly used to be able to control groups of sprites at the same time, similar to layers in Photoshop. Sprites can have other sprites as children, which gives us the opportunity to create things consisting of multiple sprites, such as a tank consisting of a body and a barrel.

No comments:

Post a Comment