Game Development Reference
In-Depth Information
UITapGestureRecognizer* tapRecognizer = [[UITapGestureRecognizer alloc]
initWithTarget:self action:@selector(tapGesture:)];
[tapRecognizer setNumberOfTapsRequired:1];
[tapRecognizer setNumberOfTouchesRequired:1];
[coinsView addGestureRecognizer:tapRecognizer];
}
In the viewDidLoad task, we have several small items to attend to. First, we call the super
implementation of viewDidLoad . This is not strictly necessary but is just good practice. If we ever
wanted to change the super class of CoinsController , we might spend a lot of time figuring out why
things are not initializing correctly.
viewDidLoad is to set up some UIView instances to do what we want.
4-6 , we see that there is a partially transparent border around the white area
UIView called coinsView , which is the parent view for the views that
coinsView based on the frame of the parent view. On the
coinsViewWidth turns out to be 300 points; on the iPad, coinsViewWidth is
coinsView to white, we set the property clipToBounds
viewDidLoad is to register a gesture recognizer to the coinsView .
In this case, we want to know anytime the user taps on the coinsView , so we use a
UITapGestureRecognizer called tapRecognizer . When tapRecognizer is initialized, we set self as the
target and specify the task tapGesture: as the task to be called when tapRecognizer detects the tap
gesture. By setting the number of required taps and the number of required touches, we make sure
we pick up the correct gestures that the user makes. Adding tapRecognizer to coinsView finishes
setting up our gesture recognizer. More information about how gestures work on iOS can be found in
Chapter 8.
Starting a New Game
After viewDidLoad is called, the CoinsController will be used either to play a new game or to
continue an old game. Let's take a look at the task newGame first, as shown in Listing 4-9.
Listing 4-9. CoinsController.m (newGame)
-(void)newGame{
for (UIView* view in [coinsView subviews]){
[view removeFromSuperview];
}
[coinsGame release];
coinsGame = [[CoinsGame alloc] initRandomWithRows:5 Cols:5];
 
Search WWH ::




Custom Search