Game Development Reference
In-Depth Information
The first parameter is the target of the glitter field, which is of type Texture2D .Weare
going to use this object to determine where we can place our glitters in the rectangle
(we will talk more about that later on). The second parameter, density , indicates how
many glitters can be visible at the same time. Then, we have the width and height
parameters that indicate the size of the rectangle. We also pass along a parameter
called xoffset . We will deal with that one later on as well. Finally, we have the layer
and id parameters, which are passed on to the base constructor.
Inside the constructor, we store most of these parameters in member variables,
so we can access them later:
this .glitter = JewelJam.AssetManager.GetSprite("spr_glitter");
this .target = target;
this .xoffset = xoffset;
this .width = width;
this .height = height;
18.4.2 Adding Glitters
A glitter field is a rectangle containing multiple glitters, depending on the desired
density. Therefore, we are going to need a list to maintain where these glitters
should be drawn. This is done in the member variable positions , which is of type
List<Vector2> , and it is initialized in the constructor:
positions = new List<Vector2>();
We are going to fill this list with a number of randomly generated positions. For
that, we use a for -instruction, and we call the method CreateRandomPosition , which
we will discuss later in more detail:
for ( int i = 0; i < density; i++)
positions.Add( this .CreateRandomPosition());
For drawing the glitters, we need more than just a position. We want to add a nice
visual effect that lets the glitters appear and disappear smoothly. We will achieve
that by drawing the glitters at a first increasing and then decreasing scale. So, this
means we also need to maintain the current scale for each of the glitters that we
are drawing. We do this in another list of float variables called scales , which is also
initialized in the constructor:
scales = new List< float >();
Every time we add a new position to the positions list, we also add a scale of 0 to the
scales list. So the final for -instruction becomes
 
Search WWH ::




Custom Search