Online Book Reader

Home Category

iPhone Game Development - Chris Craft [71]

By Root 1514 0
pixels every second, try moving the road down a pixel every tenth of a second. To do this, you will need to first update the NSTimer code to fire 10 times a second by making the following change:

// start a timer that will fire every tenth of a second

[NSTimer scheduledTimerWithTimeInterval:(0.1) target:self selector:@selector(onTimer) userInfo:nil repeats:YES];

Next, update the randomRoadUpdate function to only move 1 pixel instead of 10 by making the following modification:

- (void)randomRoadUpdate {

CGPoint oldPosition = road.center;

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

}

Save and run your update code. You should experience a much smoother animation now. Feel free to modify the code yourself and experiment with different settings to see what outcome they produce. There is more than one way to do just about anything. If you are always looking for a better way to do something, you will learn so much more and your programs will be all the better for it. Not to mention, it's fun to play the role of the mad scientist and see what happens!

Random numbers

Our original goal was to have the road move left and right by a random amount. To do this correctly, we will have to use the random() method. Here is the code to pick a random number from 0 to 9:

int randomNumber = random() % 10;

Tip

Random numbers are responsible for a lot of the magic in many games, especially action games. Look for ways you can use random values to create variety and surprises in your games for players.

The percent (%) sign is used to denote the modulus operator. This means divide the first number by the second number and return the remainder. It is a clever math trick to perform a useful operation. Here's an example. Pick any ten-digit number; let's say 3141592653. So, 10 can be divided into 3141592653, a total of 314159265 times with a remainder of 3. This is effectively how we are using the random() method to create random numbers. The random() method picks a huge random number and we scale this down to the range we need by using the modulus operator.

What if we needed a number from –5 to 5? How could we use the random function to achieve this? One way is to use the following line of code. Since –5 to 5 includes 11 numbers, we change the 10 to an 11. Then we subtract 5 from the result to make the range from 0 to 10 to the range of –5 to 5:

int distance = (random() % 11) - 5;

Modify the randomRoadUpdate method to take advantage of what you learned in the preceding section. Instead of moving the road vertically, change the code to move the road horizontally. When you have finished making your changes, your randomRoadUpdate method should look similar to the following code:

- (void)randomRoadUpdate {

int distance = (random() % 11) - 5;

CGPoint oldPosition = road.center;

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

}

Again, feel free to make any changes of your own to the code. It's a great way to learn more. You now have a road object that can move to the left and to the right. This means the player is going to have to pay attention or risk running off the road. There is one potential snag in the road's behavior that could be cause for concern. It is possible for the road to move in one direction more than the other, even to the point of moving off the screen. You want to limit this behavior. If a player has to keep his car on the road, the least you can do is keep the road on the screen. It's only fair.

You need to test the road position and check if moving it will move it outside of a certain allowed range. If so, you want to block this move. Start out by only allowing the road to move 20 percent of the screen's width to the left or to the right of the center of the iPhone's screen. Since the iPhone is 320 pixels wide, its center is at 160 pixels from the left. Ten percent of 320 is 32; therefore, 20 percent is 64, and 160 minus 64 is 96, and 160 plus 64 is 224. This gives you an allowed range of 96 to 224. If moving the road by a random distance is

Return Main Page Previous Page Next Page

®Online Book Reader