Calvin's

Icon

designs and hacks. people and products.

CCDirector

Cocos2d’s Director class – CCDirector – is the pillar in the cocos2d game engine.  It is called in our AppDelegate.m file in the applicationDidFinishLoading method as:

CCDirector *director = [CCDirector sharedDirector];

This Director is a singleton because it stores global configuration settings for cocos2d and manages the cocos2d scenes.  The Director is responsible for:

1. Access to and changing scenes
2. Access to cocos2d configuration details
3. Access to views (OpenGL, UIView and UIWindow)
4. Pausing, resuming and ending the game
5. Converting UIKit and OpenGL coordinates

There are four different types of Directors that we can use.  The most common one is CCDisplayLinkDirector (only available for iOS 3.1 or higher).  The alternative is the CCFastDirector.  If we are using Cocoa Touch views along with cocos2d, then we will have to use CCThreadedFastDirector because it is the only Director that fully supports them.  CCThreadedFastDirector consumes the device’s battery rapidly so be careful about that!  CCTimerDirector is the last resort if we want to avoid consuming the device battery rapidly but it has the disadvantage of being slower with its speed.

cocos2d Singleton

Cocos2d uses the Singleton design pattern.

A singleton is a regular class which is instantiated only once during the lifetime of an application.  To do so, a static method is used to both create and access the instance of the object.  Instead of alloc/init, new or a static autorelease initializer, we gain access to a singleton object via methods which begin with shared. Examples of cocos2d singleton classes:

CCActionManager* sharedManager = [CCActionManager sharedManager];
CCDirector* sharedDirector = [CCDirector sharedDirector];
CCSpriteFrameCache* sharedCache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCTextureCache* sharedTexCache = [CCTextureCache sharedTextureCache];
CCTouchDispatcher* sharedDispatcher = [CCTouchDispatcher sharedDispatcher];
CDAudioManager* sharedManager = [CDAudioManager sharedManager];
SimpleAudioEngine* sharedEngine = [SimpleAudioEngine sharedEngine];

A singleton acts like a global class, behaving in the same way as global variables do. They are normally used when we have a combination of data and methods that we need in many places. For a game app with multiple levels, we use singletons to store information which can be carried over from one level to another. Here’s an example of a singleton implementation:

static MyManager *sharedManager = nil;

+ (MyManager*) sharedManager
{
if (sharedManager == nil)
{
sharedManager = [[MyManager alloc] init];
}
return sharedManager;
}

The danger of relying on singletons is that we may end up using it in situations where they aren’t appropriate.

Before we create a singleton, it is important to consider if we really need only one instance of this class and its data globally and whether this might be something that could change in the next version of our app implementation!

 

Anatomy of a cocos2d project

anatomy of a cocos2d project

The two classes that make up the core of a new cocos2d project are the AppDelegate class (AppDelegate.h and AppDelegate.m) and the HelloWorldLayer class (HelloWorldLayer.h and HelloWorldLayer.m).

The AppDelegate class handles our app’s global events and state changes while the HelloWorldLayer handles the code which displays the Hello World label.

AppDelegate

Like any other iOS app, a coco2d app has one AppDelegate class which implements the UIApplication protocol.  The AppDelegate is used to track state changes of the application and it receives messages from iOS at different points in time.  The AppDelegate is where we determine if a user is getting an incoming phone call or when the app is low on memory.

The first message that our app will receive is the applicationDidFinishLaunching method.  This is where all our startup code goes to and where cocos2d is initialized.

In this file, the few things we might want to modify for a specific cocos2d project are:

1. setDeviceOrientation
2. setAnimationInterval
3. setDisplayFPW
4. enableRetinaDisplay

HelloWorldLayer

The HelloWorldLayer class is where cocos2d does its magic to display the Hello World Label.  cocos2d uses a hierarchy of CCNode objects to determine what is displayed where.  The base class of all nodes is the CCNode class which contains a position but has no visual representation.  It is the parent class for all other node classes including CCScene and CCLayer.

CCScene is an abstract concept which allows proper placement of objects in the scene according to their pixel coordinates.  A CCScene node is always used as the parent object for every cocos2d scene hierarchy.

Read the rest of this entry »