Game Development Reference
In-Depth Information
that yet, we want to be able to display three different states with these levels buttons,
because a level can be locked, unlocked but not yet solved by the player, and solved.
For each of these different situations we will use a different sprite for displaying the
button. However, in the PenguinPairs3 example, we will simply show the 'locked'
status for each level.
So before we can create the LevelMenuState class, we will add a class called
LevelButton . This class inherits from GameObjectList . For convenience, we will also
store the level index (which is between one and twelve in this example) in a
LevelButton object. The constructor of the LevelButton class is then given as follows:
public LevelButton( int levelIndex, int layer = 0, string id = "")
: base (layer, id)
{
this .levelIndex = levelIndex;
spr_lock = new SpriteGameObject("Sprites/spr_lock");
this .Add(spr_lock);
}
In the HandleInput method, we check if the button has been pressed by checking if
the mouse position is within the bounding box of the sprite we added to the list and
the player has pressed the left mouse button:
pressed = inputHelper.MouseLeftButtonPressed() &&
spr_lock.BoundingBox.Contains(( int )inputHelper.MousePosition.X,
( int )inputHelper.MousePosition.Y);
Next to that, we also add Width and Height properties that we will need when we
place the level buttons on the screen.
Now that we have a basic LevelButton class, we can add these level buttons in
the LevelMenuState class. In this example, we will add twelve level buttons to the
menu, using a for -instruction. Depending on the value of the counter variable ( i ), we
calculate the row and column that the button belongs to. This information, together
with the width and the height of a level button, can help us to calculate the final
position of each level button:
for ( int i = 0; i < 12; i++)
{
int row = i / 5;
int column = i % 5;
LevelButton level = new LevelButton(1);
level.Position = new Vector2(column
(level.Width + 30),
(level.Height + 5))
+ new Vector2(155, 230);
row
this .Add(level);
}
Search WWH ::




Custom Search