Game Development Reference
In-Depth Information
In Listing 3-9, we see that the addScore: task takes a new Score as an argument. The newScore
object is passed onto the addScore: task of the object highscores , where it is either discarded if it
is not high enough to be a high score or inserted into the array of ten Score s stored in highscores .
We also see that the task in Listing 3-9 calls a saveHighscores task and then updates the layout of
the views by calling layoutScores :. Let's take a look at how the views are updated before we look at
saving the high scores. Listing 3-10 shows the implementation of layoutScores: .
Listing 3-10. HighscoreController.m (layoutScores:)
-(void)layoutScores:(Score*)latestScore{
for (UIView* subview in [highscoresView subviews]){
[subview removeFromSuperview];
}
CGRect hvFrame = [highscoresView frame];
float oneTenthHeight = hvFrame.size.height/10.0;
float halfWidth = hvFrame.size.width/2.0;
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyy-MM-dd"];
int index = 0;
for (Score* score in [highscores theScores]){
CGRect dateFrame = CGRectMake(0, index*oneTenthHeight, halfWidth, oneTenthHeight);
UILabel* dateLabel = [[UILabel alloc] initWithFrame:dateFrame];
[dateLabel setText: [dateFormat stringFromDate:[score date]]];
[dateLabel setTextAlignment:UITextAlignmentLeft];
[highscoresView addSubview:dateLabel];
CGRect scoreFrame = CGRectMake(halfWidth, index*oneTenthHeight, halfWidth, oneTenthHeight);
UILabel* scoreLabel = [[UILabel alloc] initWithFrame:scoreFrame];
[scoreLabel setText:[NSString stringWithFormat:@"%d", [score score]]];
[scoreLabel setTextAlignment:UITextAlignmentRight];
[highscoresView addSubview:scoreLabel];
if (latestScore != nil && latestScore == score){
[dateLabel setTextColor:[UIColor blueColor]];
[scoreLabel setTextColor:[UIColor blueColor]];
} else {
[dateLabel setTextColor:[UIColor blackColor]];
[scoreLabel setTextColor:[UIColor blackColor]];
}
index++;
}
}
Search WWH ::




Custom Search