Game Development Reference
In-Depth Information
the fire function, which uses some basic vector math to determine the direction
of the projectile.
The other functions in Ship are more or less helper functions. One sets up the
correct animation set, depending on the color of the ship, and the other generates
a list of CCMoveTo actions when given a list of points to travel toward.
ObjectLayer
The ObjectLayer is the main layer that controls gameplay, and you'll notice a
lot of member variables in this class. Most notably, it has a pointer to the Ship
and arrays for the player's lasers, the enemies, and the enemy projectiles. Beyond
that, several parameters are used to control the basic behavior of the game, such
as the speed of the ship, the respawn time, the interval between enemy spawns,
and so on. These variables can be modified in order to tweak the difficulty of the
game.
The update function in ObjectLayer is responsible for updating all of the game
objects that are currently active. So it loops through the arrays that contain said
objects and executes each object's update member function. Once all of the ob-
jects have been updated, the code performs some collision checks. Using AABBs
(which were covered in Chapter 7 ), the code first checks to see if the active lasers
intersect with any enemies. If an enemy is hit by a laser, the laser dies and the en-
emy loses some health. If the enemy's health hits zero, it dies and is removed from
the world. Similarly, the lasers are checked for collisions against the enemy pro-
jectiles, andboththelaser andtheprojectile getdestroyed ifacollision isdetected.
Finally, the update function checks to see if any enemy projectiles are colliding
with the player's ship, in which case the player loses a life.
The enemy spawning is configured so there's a spawnEnemy function that's
called every X seconds. That X value decreases as the player gets further and fur-
ther in the game. When an enemy is spawned, it's placed at a random y-position
using arc4random ,whichisahigh-qualityrandomnumbergenerator.Thegame
also tracks how many enemies it has spawned, and after a certain number of en-
emies a boss enemy will spawn. A boss enemy is one of the larger enemies in yel-
low that performs a figure eight.
Incocos2d,alayercanbeconfiguredtoreceivetouchevents.Thisisdonewiththe
setTouchEnabled call toward the top of init . Once this is configured, you
can then have functions that get called based on specific touch events. When the
player first initiates a touch, a call to ccTouchBegan is triggered. The code in
Search WWH ::




Custom Search