Game Development Reference
In-Depth Information
[self prepareAudio: AUDIO_VIPER_EXPLOSION];
[self playBackgroundAudio: AUDIO_BC_THEME];
[actorsView addGestureRecognizer:tapRecognizer];
return YES;
}
return NO;
}
In Listing 12-8, we see the task
doSetup
as defined by the class
BeltCommanderController
. In this
task, we define the size of the game to be 480x320, which happens to be the native size of the
iPhone screen in points. We also set the property
isPaused
to
YES
, so nothing is animating until
doNewGame:
is called later. The next step is to indicate which classes of
Actors
we want easy access
to as the game progresses. In this case, we specify the classes
Saucer
,
Bullet
,
Asteroid
, and
Powerup
. Keep in mind that there is only one
Viper
in the game, and we have a reference to it in the
header file. The last thing we do is to create a
UITapGestureRecognizer
and add it to
actorsView
. In
this way, the task
tapGesture:
will be called when the user taps on the screen. Before any of that
can happen, we need to understand how a new game is created.
A New Game
Whether we are running the first game or the hundredth, they all start with the task
doNewGame:
, as
shown in Listing 12-9.
Listing 12-9. BeltCommanderController.m (doNewGame:)
-(void)doNewGame:(GameParameters*)aGameParameters{
gameParameters = aGameParameters;
[self removeAllActors];
[self setScore:0];
[self setStepNumber:0];
[self setScoreChangedOnStep:0];
viper = [Viper viper:self];
[self addActor:viper];
asteroids_destroyed = 0;
[self setIsPaused:NO];
[delegate gameStarted:self];
}
In Listing 12-9, we see the task
doNewGame:
, which is called by the
RootViewController
to start a
new game. In this task, we set the
gameParameters
for future reference. Then we have to reset this
object so that it is ready for a new game. To remove all
Actors
, we call
removeAllActors
. We also
want to reset the score, the number of steps, and the number of Asteroids destroyed. We also create