iPhone Game Development - Chris Craft [55]
The viewDidLoad method should look like the following code:
/*
If you need to do additional setup after loading the view, override viewDidLoad.
- (void)viewDidLoad {
}
*/
FIGURE 4.17
Xcode project with all images added
Modify the viewDidLoad until it matches the following version:
- (void)viewDidLoad
{
// create the view that will execute our animation
UIImageView* campFireView = [[UIImageView alloc]
initWithFrame:self.view.frame];
// load all the frames of our animation
campFireView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@”campFire01.gif”],
[UIImage imageNamed:@”campFire02.gif”],
[UIImage imageNamed:@”campFire03.gif”],
[UIImage imageNamed:@”campFire04.gif”],
[UIImage imageNamed:@”campFire05.gif”],
[UIImage imageNamed:@”campFire06.gif”],
[UIImage imageNamed:@”campFire07.gif”],
[UIImage imageNamed:@”campFire08.gif”],
[UIImage imageNamed:@”campFire09.gif”],
[UIImage imageNamed:@”campFire10.gif”],
[UIImage imageNamed:@”campFire11.gif”],
[UIImage imageNamed:@”campFire12.gif”],
[UIImage imageNamed:@”campFire13.gif”],
[UIImage imageNamed:@”campFire14.gif”],
[UIImage imageNamed:@”campFire15.gif”],
[UIImage imageNamed:@”campFire16.gif”],
[UIImage imageNamed:@”campFire17.gif”], nil];
// all frames will execute in 1.75 seconds
campFireView.animationDuration = 1.75;
// repeat the annimation forever
campFireView.animationRepeatCount = 0;
// start animating
[campFireView startAnimating];
// add the animation view to the main window
[self.view addSubview:campFireView];
[campFireView release];
}
Now you should review this code and learn more about how it works.
Here you create a UIImageView and allocate memory for it. On the next line you assign the Main View's frame to the campFireView. The Main View's frame is the view that users see whenever they are on the Main View window. When you modify this view, users automatically see any change you make to it:
// create the view that will execute our animation
UIImageView* campFireView = [[UIImageView alloc]
initWithFrame:self.view.frame];
The UIImageView named campFireView is the real workhorse of this application. Be sure to look up the UIImageView class in Xcode's API Reference. Take a moment and right-click on the text UIImageView and then click Find Selected Text in API Reference and see for yourself. Basically, UIImageView is good for displaying a single image or a series of images as an animation. The documentation covers the details on how to perform many tasks with the UIImageView class, including initializing a UIImageView, animating images with a UIImageView, and enabling and disabling user interaction on UIImageViews.
In this step you are loading all the animation images from the resource image file you added earlier. The animationImages property of UIImageView expects an NSArray of UIImage objects. You must set the last element of the NSArray to nil so that the array will know when the collection ends:
// load all the frames of our animation
campFireView.animationImages = [NSArray arrayWithObjects:
[UIImage imageNamed:@”campFire01.gif”],
[UIImage imageNamed:@”campFire02.gif”],
[UIImage imageNamed:@”campFire03.gif”],
[UIImage imageNamed:@”campFire04.gif”],
[UIImage imageNamed:@”campFire05.gif”],
[UIImage imageNamed:@”campFire06.gif”],
[UIImage imageNamed:@”campFire07.gif”],
[UIImage imageNamed:@”campFire08.gif”],
[UIImage imageNamed:@”campFire09.gif”],
[UIImage imageNamed:@”campFire10.gif”],
[UIImage imageNamed:@”campFire11.gif”],
[UIImage imageNamed:@”campFire12.gif”],
[UIImage imageNamed:@”campFire13.gif”],
[UIImage imageNamed:@”campFire14.gif”],
[UIImage imageNamed:@”campFire15.gif”],
[UIImage imageNamed:@”campFire16.gif”],
[UIImage imageNamed:@”campFire17.gif”], nil];
In this line you are informing