Game Development Reference
In-Depth Information
currently selected. These are all the member variables in the
SpriteSheet
class:
protected
Texture2D sprite;
protected int
sheetIndex;
protected int
sheetColumns;
protected int
sheetRows;
protected bool
mirror;
In the constructor, we initially assign the value 1 to the
sheetColumns
and
sheetRows
variables. The sheet index is passed along as a parameter and has 0 as the default
value (in other words, the first element in the sheet). We also retrieve the sprite using
the asset manager:
sprite = PenguinPairs.AssetManager.GetSprite(assetname);
this
.sheetIndex = sheetIndex;
this
.sheetColumns = 1;
this
.sheetRows = 1;
Now we can use the
string
.Split
method to find out if we need to modify the
column and row sizes. The
Split
method splits a string into substrings and we can
pass along one or more characters that are used as delimiters. Here we split the string
using the '@' sign as a delimiter:
string
[] assetSplit = assetname.Split('@');
As you can see, the result of calling the
Split
method on a string is an array of
string
objects. If the delimiter character does not appear in the string, the number of
elements in the
assetSplit
variable will be 1 (the original string). So, if the length of
the
assetSplit
array is greater than 1, we know that the file name contains information
about the dimensions of the sheet. What we also know in that case, is that the
last
element
of the array contains that information. We can easily handle this in an
if
instruction:
if
(assetSplit.Length <= 1)
return
;
// we're done
string
sheetNrData = assetSplit[assetSplit.Length
−
1];
// deal with the sheet dimension data
As you can see, we store that last element in a new local variable which now contains
the string describing the sheet dimensions. We use the
Split
method one more time,
to extract the actual dimensions from that string:
string
[] colrow = sheetNrData.Split('x');
Again, there are two possibilities here. The
colrow
array contains either one or two
elements. In both cases, we have to convert the first string element in the array to an
int
and store it in the
sheetColumns
variable: