Online Book Reader

Home Category

iPhone Game Development - Chris Craft [35]

By Root 1587 0
can be moved around, but they are not part of the solution. Early levels should have many blank tiles.

Arrow is used to define a tile with an arrow on it; these tiles have to be in the correct location to solve the puzzle.

The following code defines these types for us:

typedef enum {

empty,

blank,

arrow

} TileType;

The Tile itself is very simple; it is just a struct used to hold the data elements for the tile. You may have noticed that we mentioned a tile index, but it is not present here. The index is the same index used to store the item in its collection. Therefore, to keep from creating redundant data, we do not store that information again here.

Here is the definition you will see in the code for the Tile:

typedef struct {

int location;

TileType tileType;

} Tile;

Caution

Duplication of data can lead to mistakes and confusion. In this case we chose not to store the index because we can just use the index we used to pull an item out of the tile array. If we were to store the index in the struct, we would have to be extremely careful to always update or set this index when necessary. By choosing not to store it again, we eliminate this concern.

Now that you have seen tiles, you can understand how they can be used to construct a level. Levels contain a collection of tiles, among other parameters. One of these other parameters is difficulty. We discussed difficulty earlier and decided to create three different levels. Here is the listing for the enum Difficulty:

typedef enum {

bachelors,

masters,

doctorate

} Difficulty;

Like the Tile, the Level is just a simple struct used to hold the data elements for the level. Here is what the definition looks like:

typedef struct {

Difficulty difficulty;

int tileSize;

int gridSize;

Tile tiles[16];

} Level;

That's it for type definitions, but we have left out an important piece. Where does the data come from? This is really up to you. We have chosen to use a static array to store all of the level data. When the game instance is initialized, we rip through the static array and load our collections, tiles, and levels. As you can imagine, this is very straightforward. However, if you are interested in the details, please download the source if you have not already and take a look. You can download the code from http://appsamuckcom/amuckslider.

Building the Menu View

Now we need to erect a menu so the user can start the game, select a difficulty level, and all those good things. In puzzle games, it is at times best to lock the player out of advanced levels at first and have them build up to them. If you fail to do this, many players will rush to the difficult levels first and quickly get bored with the game. There is just something about the way we are programmed—if we are challenged to defeat levels in sequence, we are always lured by the temptation to attempt just one more level. So in the spirit of keeping the challenge high, we have chosen to require the player to defeat the levels in sequence the first time through. Once a challenge has been beaten, a player can go back and replay a previous level.

You could take this idea a step further by allowing the player to achieve badges for accomplishing specific items. A badge system keeps players coming back to replay existing levels after they get good at the game. For example, the second time through the game would challenge the player to try and finish faster or with fewer moves. You could award an “Ace” badge if the player beats a level with minimal moves or award a “Speedster” badge if he beats the level in record time. But before any of this is possible, we must begin by setting up a menu system.

Follow these steps to create the Menu View by adding a new view controller and view XIB:

1. Under Groups & Files, choose the root project node. In our case it is the topmost node named AmuckSlider.

2. From the menu choose Project⇒New Group. Name the new group Menu View.

3. From the menu choose File⇒New File to launch the New File dialog box.

4. From the New

Return Main Page Previous Page Next Page

®Online Book Reader