0

The code I've written displays the sf::Drawable objects only for the top state of the state stack. Rendering works fine for everything, except the sf::Text type, that does not change the color of the text when button.getText().setFillColor(sf::Color:Red) is called. However, when I construct a button with a red text, whenever I try to set another color to that button, I only get a white text, no matter what color I request.

Here's where I change the color of a button:

void GameState_MainMenu::handleRealTimeInput()
{
    for each (TextButton button in mButtons)
    {
        if (button.isSpriteClicked())
        {
            button.getText().setFillColor(sf::Color::Red);
            button.triggerAction();
            sf::Clock wait;
            sf::Time timer = sf::Time::Zero;
            timer = sf::seconds(0.15f);
            while (wait.getElapsedTime() < timer)
            {

            }
            wait.restart();
        }
    }
}

and this is my Game::render() method:

void Game::render()
{
    GameState *currentState = getActiveState();
    if (currentState != nullptr)
    {
        mWindow.clear();
        currentState->draw();
    }
    mWindow.display();
}

Lastly, this is the draw method of the MainMenu state:

void GameState_MainMenu::draw()
{
    game->mWindow.draw(game->mBackground);
    game->mWindow.draw(mSelector.mSprite);
    for each (TextButton button in mButtons)
    {
        game->mWindow.draw(button.getText());
    }
}

2 Answers2

0

It's probably because you have a while loop in the GameState_MainMenu::handleRealTimeInput that the program is getting stuck in.

You can try to use threads, though that way could get pretty messy. I suggest revising your code.

Verideth
  • 7
  • 8
0

Okay, so I figured out this had something to do with c++'s for each instruction. As soon as I switched to the classic array-like traversal, my buttons started changing colors. I'm not saying this is THE solution, just that it worked for me. If anyone has the same problem, you might want to check that.