Online Book Reader

Home Category

iPhone Game Development - Chris Craft [75]

By Root 1533 0
will have the player lose if the car ever reaches the bottom of the screen, using a simple form of collision detection. You will make this possible by causing the player's car to slow down if it drives off the road. You can always find the road's position and the player's car position. Using this and what you know about the road, you can calculate whether the player is on the road.

FIGURE 5.19

AmuckRacer high score


Here is an overview of the math. You know the iPhone screen is 320 pixels wide, and the road is the same size. You know or you can check that the car image width is 42 pixels wide. Even though the road.png image is 320 pixels wide, the road in the image is really only 200 pixels wide. But that is not all you need to determine whether the player is keeping the car on the road. You will need to perform two checks: one to see if the car has driven off the left-hand side of the road, and the other to see if the car has driven off the right-hand side of the road.

To check if the car has driven off the left-hand side of the road, use the road image's center to find the center of the actual road, and subtract 100 to find the road's left-most edge. Compare this to the car's left-most edge to determine if the car remains on the actual road. To calculate the car's left-most edge, you use the car's center and subtract 24. You use the numbers 100 and 21 because these values are half of the widths of the road and car images, respectively. And the center of an image is at the halfway point from the left-most and right-most edges.

Now that you understand the concept, here is the code to make it happen:

- (void)updateCar {

CGPoint roadPosition = road.center;

int roadLeftPosition = roadPosition.x - 100;

int roadRightPosition = roadPosition.x + 100;

CGPoint carPosition = car.center;

int carLeftPosition = carPosition.x - 21;

int carRightPosition = carPosition.x + 21;

if(carLeftPosition < roadLeftPosition)

car.center = CGPointMake(carPosition.x, carPosition.y + 1);

if(carRightPosition > roadRightPosition)

car.center = CGPointMake(carPosition.x, carPosition.y + 1);

if(carLeftPosition > roadLeftPosition && carRightPosition < roadRightPosition)

car.center = CGPointMake(carPosition.x, carPosition.y - 1);

}

Make sure you add this call to your main update method:

- (void)update {

[self updateRoad];

[self updateCar];

}

Don't forget to update your MainView.h header file with the new method name:

#import

@interface MainView : UIView {

IBOutlet UIImageView *car;

IBOutlet UIImageView *road;

IBOutlet UILabel *score;

}

- (void)onTimer;

- (void)update;

- (void)updateRoad;

- (void)updateCar;

- (void)randomRoadUpdate;

- (void)draw;

- (void)drawScore;

@end

What's Next?

You have been exposed to a lot of ideas and material in this chapter. You are probably already thinking of a few ideas of your own. Everything we have covered in this chapter is great material to take and build upon to create even bigger and better ideas. Here are a few for your consideration:

Convert the road into water and the car into a boat. Only the center of the water is deep enough for the boat to go fast.

Convert the road into outer space and the car into a spaceship. The edge of the screen is filled with asteroids that cause the spaceship to slow down.

Convert the road into snow and the car into a skier. Only the center of the snow is free from trees to allow the skier to ski his fastest.

Add oil slick images to the game and if the car hits an oil slick, it ignores player input for 2 seconds.

Add additional cars to the game that force the player to drive around them to avoid collisions.

Have random nitro canister power-ups that the player can acquire to gain a temporary speed boost.

Create a variable to track how far the player can maintain on-road status. If the player does very well, give a score bonus and an extra speed boost.

Feel free to use these ideas along with any you may have to “power up” a

Return Main Page Previous Page Next Page

®Online Book Reader