Game Development Reference
In-Depth Information
of instructions, as you did in the example with cases 3 and 4. The syntax of the
switch
instruction is
part of the
instruction
syntax diagram. Figure
21-1
shows the part of that diagram belonging to the
switch
instruction.
switch instruction
switch
(
expression
)
{
default
declaration
case
expression
:
}
instruction
Figure 21-1.
Syntax diagram for the
switch
instruction
Loading Different Kinds of Tiles
You can use the
switch
instruction to load all the different tiles and game objects. For each
character in the
levelData.tiles
variable, you need to perform a different task. For example, when
the character “.” is read, you need to create a normal playing-field tile. The following instructions
do that:
t = new Tile(sprites.field, ID.layer_objects);
t.sheetIndex = row + col % 2;
tileField.addAt(t, col, row);
break;
The sprite used for the tile is a strip consisting of two different sprites. By switching the sheet index
using the formula
row + col % 2
, you get an alternating checkerboard pattern, as you can see by
running the example program belonging to this chapter. Another example is adding a transparent
background tile:
t = new Tile(sprites.wall, ID.layer_objects);
t.type = TileType.background;
tileField.addAt(t, col, row);
break;
Although the background sprite is invisible, you still load a sprite belonging to this tile. Why is that?
Because the
Tile
class inherits from the
SpriteGameObject
class, which requires a sprite. Of course,
another option would be to modify the
SpriteGameObject
class so that it can deal with a sprite that is
null
. However, in this case, you follow the simple solution of providing a sprite, even if the player will
never see it. When you have to place a penguin, two things need to be done:
Place a normal tile.
Place a penguin.
Search WWH ::
Custom Search