Game Development Reference
In-Depth Information
Listing 6-17. ImageRepresentation.m (getViewForActor:In:)
-(UIView*)getViewForActor:(Actor*)actor In:(GameController*)aController{
if (view == nil){
UIImage* image = [self getImageForActor: actor];
view = [[UIImageView alloc] initWithImage: image];
}
return view;
}
I mageRepresentation simply tests to see whether view has been created, and creates it if it has not.
To create the UIImageView , we call getImageForActor : and then create UIImageView with the UIImage
that was returned. The task getImageForActor : is shown in Listing 6-18.
ImageRepresentation.m (getImageForActor:)
NSString* imageName = [self getImageNameForActor:actor];
UIImage* result = [UIImage imageNamed: imageName];
if (result == nil){
NSLog(@"Image Not Found: %@", imageName);
}
return result;
The task getImageForActor : simply calls getImageNameForActor and creates a UIImage based on
that name. If no image exists with the given name, the result will be nil. Placing a beak point on that
NSLog statement will save you a lot of trouble when debugging your app, as you will be notified
immediately when an actor is being drawn with a nonexisting image.
Updating Our Views
Now that we have looked at how ImageRepresentation creates a UIView used to draw an actor, let's
take a look at how ImageRepresentation updates that UIView to create the cycling animations and
responds to a change in state or variation. Listing 6-19 shows the task updateView:ForActor:In : for
ImageRepresentation .
Listing 6-19. ImageRepresentation.m (updateView:ForActor:In:)
-(void)updateView:(UIView*)aView ForActor:(Actor*)anActor In:(GameController*)aController{
if ([delegate respondsToSelector:@selector(getFrameCountForVariant:AndState:)]){
[self advanceFrame: anActor ForStep:[aController stepNumber]];
}
if ([anActor needsViewUpdated]){
UIImageView* imageView = (UIImageView*)aView;
UIImage* image = [self getImageForActor: anActor];
 
Search WWH ::




Custom Search