Game Development Reference
In-Depth Information
[self createAndLayoutImages];
[delegate gameDidStart:self with: coinsGame];
acceptingInput = YES;
}
The task
newGame
is called when the New Game button is clicked on the Welcome screen. The first
thing to do is remove the old subviews from
coinsView
. (There may not be any subviews to remove
if the user just launched the application.) The object
coinsGame
is released before being set to a new
instance prepopulated with random coin values. The next step is to call
createAndLayoutImages
,
which will place the coins on the screen. This task is called by both
continueGame:
and
newGame
, so
we will look at it after looking at
continueGame:
. The last thing to do is inform any delegate that a
new game has started and set acceptingInput to
YES
.
Continuing a Game
If a game was in progress when a user last quit, that user may wish to continue the game from
where it left off. This is done by clicking the Continue button in the Welcome view. When that button
is pressed, the task
continueGame
is called, which is pretty similar to
newGame
. Listing 4-10 shows
continueGame:
.
Listing 4-10. CoinsController.m (continueGame:)
-(void)continueGame:(CoinsGame*)aCoinsGame{
for (UIView* view in [coinsView subviews]){
[view removeFromSuperview];
}
[coinsGame release];
coinsGame = aCoinsGame;
[self createAndLayoutImages];
[delegate gameDidStart:self with: coinsGame];
acceptingInput = YES;
}
The task
continueGame:
takes a
CoinsGame
object as an argument called
aCoinsGame
. After clearing
away the old subviews of
coinsView
, we set
coinsGame
to the passed-in
aCoinsGame
object. A
call to
createAndLayoutImages
is made, to add the coins to the scene. We call the delegate task
gameDidStart:coinsGame:
so the delegate has a chance to update the
UILabel
objects that track the
current score and remaining turn. Finally, we set
acceptingInput
to
YES
.
Initializing the UIViews for Each Coin
As mentioned, the task
createAndLayoutImages
is called by both
newGame
and
continueGame:
. This
task is responsible for the initial setup of the game, primarily creating a
UIImageView
for each coin in
the game. This task is shown in Listing 4-11.