Game Development Reference
In-Depth Information
A core concept in cocos2d is the base CCNode class, which represents a scene
node , or an object that has a position and can be drawn onscreen. From CCNode
there are several children, including CCScene (an overall scene), CCLayer (a
specific layer in a scene), and CCSprite (a sprite object). At a minimum, a
cocos2d game must have one CCScene and one CCLayer , though most games
will have more than this.
A CCScene can be thought of as a specific state of the game. For example, in
Ship Attack there is a main menu scene as well as a gameplay scene. A CCScene
must be associated with at least one CCLayer , though it's possible to have sev-
eral CCLayers with different z-ordering. The gameplay scene has three different
layers: a far-away nebula, a main object layer, and a faster scrolling star field. You
normally also might have a separate layer for the UI, but because this game is so
simple it did not seem necessary to separate the UI.
CCSprite is used for all of the moving and interactive objects in the game. This
includes the player's ship, lasers, enemy projectiles, and enemy flying saucers.
Rather than storing each sprite in an individual file, I used sprite sheets, which
were generated via TexturePacker. Conveniently, cocos2d can automatically im-
port sprite sheets created by TexturePacker, so it makes the process of loading and
animating said sprites relatively straightforward.
Onedrawbackofusingcocos2disthatit'slimitedtoonlythetwoAppleplatforms.
However, a C++ port of the library called cocos2d-x is now available that supports
a long list of platforms, including all the major mobile and desktop ones. Although
the class and function names are almost identical in cocos2d-x, there are some dif-
ferences in how they are used simply due to language differences. For example,
cocos2d-xhasSTLatitsdisposal,whereascocos2dmustuse NSMutableArray
and the like. For Ship Attack , I chose to use base cocos2d because less document-
ation is available for the newer cocos2d-x. However, as more topics on cocos2d-x
enter the market, this will become less of an issue.
Code Analysis
Nowthatwe'vecoveredthetechnologies behind Ship Attack ,wecantakeadeeper
lookatthesourcecodeandhowitssystems areimplemented. Overall, thecodefor
this game is definitely simpler than the code for the game in the following chapter.
This makes sense because the mechanics of Ship Attack are far simpler than that
of the tower defense game in Chapter 14 , “ Sample Game: Tower Defense for PC/
Mac .”
Search WWH ::




Custom Search