Game Development Reference
In-Depth Information
Also, we will add a property that computes the number of elements in the sheet,
which is defined as the number of columns times the number of rows:
public int NumberSheetElements
{
this .sheetRows; }
get { return this .sheetColumns
}
Yet another property is used for retrieving and setting the currently selected ele-
ment in the sheet. The selected element should be within the bounds of the possible
element indices. We check this inside the set part of the property:
public int SheetIndex
{
get
{
return this .sheetIndex;
}
set
{
if ( value < NumberSheetElements && value >= 0)
this .sheetIndex = value ;
}
}
Furthermore, we add a property Mirror to control whether the sprite should be drawn
mirrored or not, and we add a property Center that calculates the center of the sprite.
The next step is being able to draw the correct element in the sprite. This is done
in the Draw method of the SpriteSheet class. As a first step, we need to convert the
sheetIndex value to column and row indices in the sheet. We calculate the column
index as follows:
int columnIndex = sheetIndex % sheetColumns;
Basically, you can see the sheet index as a value that passes through all the elements
in the sheet from left to right, top to bottom. So by applying the modulus operator on
the sheet index, we 'throw away' the rows coming before the row that the element
is in which leaves us with the column index. Similarly, we calculate the row index
by dividing the sheet index by the number of columns:
int rowIndex = sheetIndex / sheetColumns;
Now we can construct the rectangle that indicates the part of the sprite that should
be drawn, using the Width and Height properties:
this .Width,
Rectangle spritePart = new Rectangle(columnIndex
this .Height, this .Width, this .Height);
rowIndex
Search WWH ::




Custom Search