5SD033 Tutorials

Badly Oriented Tutorial ButtonsThis week has mostly seen smaller fixes in our codebase, as we are essentially feature-frozen. However as feedback has continously asked for better explanations ingame on how to actually play the game, this post will detail how I have implemented our TutorialButton class, that will display a sprite on screen and remove it once a player presses the appropriate button. It is not a hard lesson, but there really is not much else to speak of this week as pretty much all important systems are already in the game or being tweaked by other members of our team.

Classes all on their own

We needed a tutorial, but we did not exactly have the time to create a proper manager for it as that time was needed to apply final fixes to unused code, High Score and so on. We did not however want more hardcoded entries in the Game State, and so I decided to make the buttons that appeared as their own class, not functions in the state itself.

The object would need a sprite (not a pointer to one as each button is different), an assigned key , a position on screen and a rotation, all set as arguments (since the objects always appear at the same position, are only ever created once, and should not be re-used after the tutorial portion is over).

After that, a bool is assigned as true in the constructor, that keeps track of wether a button is to be shown or not. The object’s Draw function is run in our Gamestate’s draw portion, and executes if the aforementioned bool is true.

Quick teaching

To get the button not to show up on the player screen anymore, an SFML event is created and checked via the isKeyPressed function of that event, checking against the assigned key or simply setting its assigned bool directly, as seen in the case below:

case sf::Event::KeyPressed:
switch (e.key.code)
{
case sf::Keyboard::Escape:
marketValue = 0;
break;
case sf::Keyboard::Space:
if (spaceButton->getShown() == true)
{
spaceButton->setShown(false);
}
turretManager.AddTurret(player.getPosition(), player.getRotation(), marketValue);
break;
case sf::Keyboard::A:
if (aButton->getShown() == true)
{
aButton->setShown(false);
}
break;
case sf::Keyboard::D:
if (dButton->getShown() ==true)
{
dButton->setShown(false);
}
break;

[/cpp]

As seen above, since there’s no need to constantly set the value to false after it has already been set once, I check if it has already been set via a Getter function that checks the appropriate bool value.

Because of the limited scope of these objects, the may as well be created as pointers, deleted when their use is over.

This will be my last blog post for this course, and it has been quite a learning experience. If this is the person reading this’ final blogpost to review, please do read some of my older posts regarding subjects like enemy tracking and movement, high score lists and screenclearing effects.

About Karl Malm

2015 Programming