iPhone Game Development - Chris Craft [69]
Update your awakeFromNib method so that it matches the following code:
-(void)awakeFromNib {
[[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO];
// start a timer that will fire every second
[NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];
}
Now add the following onTimer method right below the awakeFromNib method. We will update this method as we progress through the creation of AmuckRacer:
- (void)onTimer {
}
You'll notice when you are working with Xcode that it always creates a pair of files for you to write code in. The first is an h file, which is a header file; and the second is the m file, which is the main file. By default, Xcode expects you to implement your code as an interface. This means you will list your methods inside your header file as a contract, and then you will implement your methods fully in your main file. Let's update our header file to list our new functions so Xcode will be able to understand our class:
#import @interface MainView : UIView { IBOutlet UIImageView *car; IBOutlet UIImageView *road; } - (void)onTimer; @end You use NSTimer objects to create timers in Xcode. In this case we are using the scheduledTimerWithTimeInterval to specify we want the timer to call the onTimer method every 1 second. This will give us an effective frame rate of about one frame per second. That's not very fast by today's gaming standards, but don't worry, we'll decrease our time interval as we move forward. Note that you cannot simply change the time interval from 1.0 to 0.001 to get 1,000 frames per second unless the hardware is fast enough to allow it. If the hardware is not fast enough to perform 1,000 frames per second, it will either drop requests or potentially cause the system to become unresponsive and possibly unstable. Think about how elevators work. More people pressing the same call elevator button does not necessarily make the elevator come more quickly. The elevator has a hardware-limited maximum speed, and so does the iPhone. Something else you should be aware of is that all devices are not created equal. The iPhone Simulator on your computer is likely to have access to more powerful hardware overall than an actual iPhone device. This is why it is important to performance test with an actual iPhone device. It's fine to do most of your development on the iPhone Simulator, just be sure to do periodic testing on a real device to ensure both performance and quality. Since the iPhone Simulator is not a true emulator, it is possible to have bugs on a real device that you do not see on the Simulator. Also, be aware that newer devices like the iPhone 3GS can be significantly faster than earlier devices. Caution While you can write software for the iPhone using only the Simulator, you risk releasing a product that will not run when installed on an actual device. Always test software on a real device before submitting it to the App Store. You should now update your onTimer method to include calls to new update and draw methods that you will add next. Update your MainView.m file to match Listing 5.2. Listing 5.2 Adding the onTimer Method to the MainView.h File #import “MainView.h” @implementation MainView -(void)awakeFromNib { [[UIApplication sharedApplication] setStatusBarHidden:YES animated:NO]; // start a timer that will fire every second [NSTimer scheduledTimerWithTimeInterval:(1.0) target:self selector:@selector(onTimer) userInfo:nil repeats:YES]; } - (void)onTimer { update(); draw(); } - (void)update { } - (void)draw { } - (id)initWithFrame:(CGRect)frame { if (self = [super initWithFrame:frame]) { // Initialization code } return self; } - (void)drawRect:(CGRect)rect