Game Development Reference
In-Depth Information
Listing 12-16. BeltCommanderController.m (doUpdateHUD)
-(void)doUpdateHUD{
if ([self stepNumber] == [self scoreChangedOnStep]){
[scoreLabel setText: [[NSNumber numberWithLong:[self score]] stringValue] ];
}
[healthBarView setHealth:[viper health]/[viper maxHealth]];
}
In Listing 12-16, we see the task doUpdateHUD that is called for every step of the game. In this task,
we check to see if the current stepNumber is equal to the property scoreChangedOnStep . The property
scoreChangedOnStep is the value of stepNumber when the score was last updated. So, if stepNumber
is equal to scoreChangedOnStep , we know that the score was changed in this step and, hence,
scoreLabel needs to be updated. To update scoreLabel , we simply call setText: and pass in an
NSString version of the score.
In Listing 12-16, we also call setHealth: on healthBarView to update how the health bar is drawn.
The object healthBarView is of type HealthBarView , which is simply a UIView with a custom
drawRect: task, as shown in Listing 12-17.
Listing 12-17. HealthBarView.m (drawRect:)
- (void)drawRect:(CGRect)rect
{
[self setDefaults];
int index = 0;
float marker = [[percents objectAtIndex:index] floatValue];
while (percent > marker) {
marker = [[percents objectAtIndex:++index] floatValue];
}
UIColor* baseColor = [colors objectAtIndex:index];
const float* rgb = CGColorGetComponents( baseColor.CGColor );
UIColor* frameColor = [UIColor colorWithRed:rgb[0] green:rgb[1] blue:rgb[2] alpha:.8f];
UIColor* healthColor = [UIColor colorWithRed:rgb[0] green:rgb[1] blue:rgb[2] alpha:.5f];
[frameColor setStroke];
[healthColor setFill];
CGSize size = [self frame].size;
CGContextRef context = UIGraphicsGetCurrentContext();
CGRect frameRect = CGRectMake(1, 1, size.width-2, size.height-2
);
CGContextStrokeRect(context, frameRect);
Search WWH ::




Custom Search