Game Development Reference
In-Depth Information
Obviously, the main difference between colored dice and colored blocks is that dice have value .
That value is represented by the face of the die. Avid role-playing gamers should know that dice
can have faces that range from 4 to 20 sides and more. However, for this game, we are using
standard 6-sided dice with values ranging from one to six. To represent value in the Die class, we
will create a property named dieValue .
public var dieValue:Number;
We also need to create the color values for the dice. Recall that there will be three different
colored dice in Dice Battle: white, blue and green. To support this, we will create DIE_COLOR_XXX
static variables that can be referenced outside the Die class:
public static var DIE_COLOR_WHITE:int = 0;
public static var DIE_COLOR_GREEN:int = 1;
public static var DIE_COLOR_BLUE:int = 2;
In the constructor for the Die class, we need to set the value and find the proper tile to display for
that Die . The first part is really easy. We just set this.dieValue to the parameter dieValue :
this.dieValue = dieValue;
However, finding the proper face tile in the tile sheet is a bit trickier. Here is the code that does it:
var tile:int = (dieValue-1) + (dieColor)*6;
This code needs a bit of illustration. Let's say that the Die constructor is passed the following
value for dieColor and dieValue:
dieValue : 5
dieColor : DIE_COLOR_GREEN (which equals 1)
Notice in Figure 9-5 that the dice in the TileSheet are in numerical order with each color in a
separate row. What we need to do to find the correct tile is to calculate the proper column location
in proper row. Recall that the tiles in a TileSheet are in a zero-based array. That means that we
have to subtract 1 from the value to find the right place in a row for a die. Thus, the first part of the
calculation is dieValue-1 .
Next, we need to find the proper row for the color of the Die . Since the colors static const values
start at 0 ( DIE_COLOR_WHITE = 0; ), if we add dieColor*6 , we will find the proper row.
In our example, dieValue equals 5 and dieColor equals 1. Plugging those values into the
equation we get (5 - 1) + (1 * 6) = 10.
Again, since the tiles are in a zero-based array, the chosen tile would be the eleventh one in the
tile sheet, as illustrated in Figure 9-5.
Search WWH ::




Custom Search