Online Book Reader

Home Category

iPhone Game Development - Chris Craft [70]

By Root 1576 0
{

// Drawing code

}

- (void)dealloc {

[super dealloc];

}

@end

In the update method you will add an updateRoad method. While breaking each part out like this does make the code a little wordier, by keeping things separated we increase organization and decrease complexity, which is basically a win-win situation. Modify your MainView.m to include and call the updateRoad method. When you are done, your update method and updateRoad method should look like the following code listing:

- (void)update {

updateRoad();

}

- (void)updateRoad {

}

You will also need to update your MainView.h header file so Xcode will be able to match your class interface. Make sure your header file matches the following code listing:

#import

@interface MainView : UIView {

IBOutlet UIImageView *car;

IBOutlet UIImageView *road;

}

- (void)onTimer;

- (void)update;

- (void)updateRoad;

- (void)randomRoadUpdate;

- (void)draw;

@end

We now have the basic scaffolding in place for the application. The code you are going to add now will be the real muscles of the program. This code will move the road image to the left and to the right to create a more realistic winding road effect. There are a lot of ways this effect could work, but let's consider two:

The road could move a random amount to the left or right.

The road could move a random amount but always toward one direction until it reached the edge of the screen, then it could reverse direction and repeat the effect.

This first pattern could be the random road update pattern. The second pattern could go by the more exciting name of the sidewinder road update pattern. We will cover how to create both of these road update effects and then you can decide which to use in your version of AmuckRacer.

Consider the following method:

- (void)randomRoadUpdate {

CGPoint oldPosition = road.center;

road.center = CGPointMake(oldPosition.x, oldPosition.y + 10);

}

The first thing this code does is save the road's center position to a variable named oldPosition. Then it creates a new center position that is 10 pixels further down from the old position. The first line inside the method creates a variable of type CGPoint named oldPosition. A CGPoint object is a structure that contains a point in a two-dimensional coordinate system. It has two fields: One is named x and holds the x-coordinate of the point, and the other is named y and holds the y-coordinate of the point.

Add this method to your version of your AmuckRacer's MainView.m file under the existing updateRoad method. Be sure to update your MainView.h file as well. You will also need to update your updateRoad method to call the new randomRoadUpdate function. You can do this by adding the following line of code to the updateRoad method:

[self randomRoadUpdate];

When you are done making all of these changes, your MainView.m file should match Listing 5.3.

Listing 5.3

Adding the randomRoadUpdate 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 {

[self update];

}

- (void)update {

[self updateRoad];

}

- (void)updateRoad {

[self randomRoadUpdate];

}

- (void)randomRoadUpdate {

CGPoint oldPosition = road.center;

road.center = CGPointMake(oldPosition.x, oldPosition.y + 10);

}

- (void)draw {

}

- (id)initWithFrame:(CGRect)frame {

if (self = [super initWithFrame:frame]) {

// Initialization code

}

return self;

}

- (void)drawRect:(CGRect)rect {

// Drawing code

}

- (void)dealloc {

[super dealloc];

}

@end

Now save and run your changes by clicking the Build and Go button in Xcode. You should see the road image moving down the screen by 10 pixels every second—very nice! You are on your way to creating your first iPhone action game. Instead of moving the road down 10

Return Main Page Previous Page Next Page

®Online Book Reader