iPhone Game Development - Chris Craft [56]
// all frames will execute in 1.75 seconds
campFireView.animationDuration = 1.75;
Tip
Consider using variables to keep track of your animation durations. This way you can create multipliers that speed up or slow down your animations. On level one all animations take x seconds, but on level two all animations take x * level modifier seconds. This is an easy way to create slow motion and bullet time effects.
If you do the math, the 17 frames you have are being played in an animation of 1.75 seconds. You are basically getting about 10 frames per second here. That's a little low, but if you watch the animation itself, it seems to work out just fine. A possible enhancement to this application would be to add more frames to the animation, but the tradeoff would be a larger final application size.
In the following code, you will set the animation repeat count, start the animation, and assign it to the application's Main View. This line sets the animation repeat count, which does exactly what you would expect. By setting it to 0 you are telling the iPhone SDK to repeat the animation forever:
// repeat the annimation forever
campFireView.animationRepeatCount = 0;
The UIImageView's method startAnimating is used to start the animation playing. You can also call stopAnimating if you wish to stop an animation. And there is an isAnimating instance method you can call in case you need to query the run-time to find out if the animation is still running:
// start animating
[campFireView startAnimating];
Now you need to take the animation you have created and add it to the main window:
// add the animation view to the main window
[self.view addSubview:campFireView];
Finally, you need to make sure that the iPhone SDK knows you are done using the campFireView so that it will release the memory for it when it finishes playing the animation or the application ends:
[campFireView release];
Tip
You can create multiple NSArray objects with different sets of images for your animations. This way when you create a new blue Bonfire animation, it's a simple matter to change all the animations over to use the new images.
It does not make sense to do so in this application, but keep in mind that you can create UIImageView definitions that have animations and that move around the screen. This is a great way to create more advanced game elements to use in other games you may create someday.
Set Xcode to Simulator | Distribution and click the Build and Go button. The application loads up in the Simulator and runs, and you should see the Bonfire application as shown in Figure 4.18.
As always, you want to look back at the path you have taken to get this application to where it is today and decide where you will take it tomorrow. You have a great reusable iPhone animation application template; as long as you can find and come up with great animations, you can keep creating exciting new iPhone applications with minimal effort. It's kind of like once you have a soda factory there is very little stopping you from creating new sodas with different flavors. The hard part is already behind you. Now all you need to do is come up with your own recipe for success.
Screensavers are great sources of inspirations for these types of applications. For example, you could create a little virtual world application that plays an animation of a daytime fantasy world during the daytime and plays a nighttime fantasy world animation at night. Even better would be to have different animations for different weather conditions and play the matching one for the user's location.
Many games have been created by a judicious usage of animations overlaid on one another. Just take animated player characters fighting animated opponents, moving over animated