Game Development Reference
In-Depth Information
If you browse all the folders in the project, you might see a huge number of files,
most of which are in the libs directory. But all of these source files are the base
cocos2d files; they are not files that were created specifically for
Ship Attack
.
Therefore, don't bother looking through those source files. You are only interested
in the handful of files that are in the main shipattack directory.
AppDelegate
The
AppDelegate
class contains
didFinishLaunchingWithOptions
,
which is the (rather verbose) entry point ofany iOS program. Forthe purposes ofa
cocos2d game, almost all of the code related to the
AppDelegate
is auto-gener-
ated. The
AppDelegate
created by the template normally launches into a scene
called
HelloWorld
. For this game, I've modified it so that the first scene it goes
into is instead the main menu.
The only other change made to the auto-generated
AppDelegate
code was to
enable multitouch, which is what allows the game to respond to multiple finger
presses. Without multitouch, the control scheme for this game would not function
properly, because one finger controls movement and the other controls firing.
MainMenuLayer
MainMenuLayer
has a class method called
scene
that returns a
CCScene
that
contains
MainMenuLayer
as its only
CCLayer
. This is the recommended ap-
proach in cocos2d when a scene has only one layer. The layer itself has very little
code in it. All it does is show the title of the game and creates a menu with one
option (to start the game). Menus can be set up in cocos2d using
CCMenu
and
CCMenuItem
, and a particular menu item can have a segment of code that is ex-
ecuted when the item is selected. In this case, when the Start Game option is se-
lected, the game transitions into the
GameplayScene
.
GameplayScene
As you might expect, the
GameplayScene
is the scene representing the main
gameplay. If you look at this file, you'll see that there are three different layers:
two
ScrollingLayers
and the
ObjectLayer
. The two
ScrollingLay-
ers
are used for the nebula and star field, whereas the
ObjectLayer
is where
all the objects in the world (as well as the score display) reside.
GameplayScene
really does not do much else other than have an update func-
tion that gets called every frame. This function, in turn, updates the layers. Finally,