Online Book Reader

Home Category

iPhone Game Development - Chris Craft [67]

By Root 1571 0

}

- (id)initWithFrame:(CGRect)frame {

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

// Initialization code

}

return self;

}

(void)drawRect:(CGRect)rect {

// Drawing code

}

- (void)dealloc {

[super dealloc];

}

@end

The basics of collision detection

Action games and collision detection go hand-in-hand. It is the rare action game that does not need some form of collision detection, and AmuckRacer is no exception. Collision detection is how games are able to tell when one game sprite makes contact with another game sprite. Many famous action games, from Asteroids to Pac-Man, use this concept.

You will need to add collision detection to AmuckRacer for several important pieces of game functionality:

If the player's car stops touching the road and begins touching the off-road area, the car will respond by decreasing speed.

If the car collides with another vehicle or an oil slick area, the car will respond by coming to a complete stop.

If the player makes it to a checkpoint finish line, the player's remaining time will be increased.

The Road Ahead

The road will be the player's primary focus, so it should be one of yours. Consider what the road needs to do:

Make the player feel that the car is in motion, and give the player the ability to accelerate and decelerate, even allowing the car to come to a complete stop.

Appear to move to the left and right over distance (very few roads are straight for too long).

Have checkpoints located along its path and end with a finish line.

Modern programming guidance recommends being agile. While agile software development practices are the topic of whole books by themselves, one of the most important principles is to create software by making small changes in many iterative steps. So, instead of trying to add code to do all three of the preceding goals at once, you should consider and code each goal separately. The wisdom of this principle is evident every time you climb a set of stairs to reach the next floor, versus climbing the exterior wall to the next floor. You might have to walk a longer distance overall, but it's much easier than to take the more direct route straight up. This is true in software development as well. It might appear to be more effort to take many small steps, but in the end it will almost always be less work.

Our first goal is to create the appearance of motion in the road. This creates the illusion that the car is in motion. This is kind of like when you are riding in a car, the car appears motionless relative to you and everything else appears to be moving instead. There are several ways to create a moving road. Let's consider a few:

3-D generated world with a road bitmap applied as a texture to large scrolling surface

2-D overhead scrolling world with road tiles added from edges of screen

Full-screen animation of road that is translated horizontally and vertically

Long bands of road images fall vertically from top of screen and move horizontally

One advantage to a 3-D generated world is the extreme flexibility it would allow. You could zoom in and zoom out, and even rotate the camera all the way around the player. But creating a professional-quality, 3-D generated world is both difficult and challenging. Of all the options, this is the hardest, the riskiest, and the most expensive to do. For the beginning iPhone developer, this is probably not the best choice.

Caution

For every 25 percent increase in problem complexity, there is a 100 percent increase in complexity of the software solution. The ultimate success of many software projects often comes down to successfully minimizing complexity.

Another option is to create a 2-D overhead scrolling game board. Since the iPhone is 480 pixels by 320 pixels, the screen could be divided into a grid of 15 squares by 10 squares, with each square 32 pixels by 32 pixels. Each of these squares, more commonly known as tiles, would then be used to draw the game board.

Then we move the game board in response to the player's actions. As tiles move off the screen, we remove them and add

Return Main Page Previous Page Next Page

®Online Book Reader