Game Development Reference
In-Depth Information
In the rare case where you want to support all four orientations, just return YES in the
shouldAutorotateToInterfaceOrientation method.
Singletons in cocos2d
Cocos2d makes good use of the Singleton design pattern, which is regularly and hotly
debated. In principle, a s ingleton is a regular class that's instantiated only once during
the lifetime of the application. To ensure that this is the case, you use a static method to
both create and access the instance of the object. So, instead of using alloc / init or
a static autorelease initializer, you gain access to a singleton object via methods that
begin with shared . The most prominent singleton class in cocos2d is the CCDir-
ector class:
CCDirector* director = [CCDirector sharedDirector];
The director itself hosts other singleton classes, which in cocos2d v1.x are separate
classes. Starting with cocos2d v2.0 you can access the CCScheduler , CCAc-
tionManager , and CCTouchDispatcher as properties of the CCDirector
CCDirector* director = [CCDirector sharedDirector];
CCScheduler* scheduler = director.scheduler;
CCActionManager* actionManager = director.actionManager;
// only when building for iOS
CCTouchDispatcher* touchDispatcher = director.touchDispatcher;
// only when building for Mac OS X
CCEventDispatcher* eventDispatcher = director.eventDispatcher;
The four cache classes in cocos2d are also implemented as Singleton classes:
CCAnimationCache* animCache = [CCAnimationCache sharedAnimationCache];
CCShaderCache* shaderCache = [CCShaderCach sharedShaderCache];
CCSpriteFrameCache* sfCache = [CCSpriteFrameCache sharedSpriteFrameCache];
CCTextureCache* textureCache = [CCTextureCache sharedTextureCache];
Each of these classes caches specific resources. Caching here means that loaded re-
sources are kept in memory even if they're no longer in use. This prevents the re-
sources from having to be reloaded from flash memory, which is relatively slow. But
Search Nedrilad ::

Custom Search