无法获取C ++ sfml来检查文本是否为蓝色

问题描述 投票:0回答:1

我正在尝试编写一个代码,检查answer2的颜色是否为蓝色,以确定用户是否得到了错误的答案,但编译器不允许在SFML中使用它

if (!next)
    {
        while (window.pollEvent(test))
        {
            switch (test.type)
            {
            case Event::Closed:
                window.close();
                break;
            case Event::KeyPressed:
                if (Keyboard::isKeyPressed(Keyboard::Num1))
                {
                    answer1.setFillColor(Color::Blue);
                    answer2.setFillColor(Color::Black);
                }

                if (Keyboard::isKeyPressed(Keyboard::Num2))
                {
                    answer2.setFillColor(Color::Blue);
                    answer1.setFillColor(Color::Black);
                }
                if (Keyboard::isKeyPressed(Keyboard::Enter))
                {
                    next = true;
                    running = true;
                }
                break;
            }
        }
    }
    next = false;
    if (!next)
    {
        answer2.getFillColor = answer;
        if (answer == "Blue")
        {
            question.setString("Correct");
        }
        else
        {
            question.setString("Wrong");
        }
    }

这是我从编译器获得的错误消息

c:\ users \只是一个人\ documents \ visual studio 2015 \ projects \ francais project \ francais project \ francais project.cpp(90):error C2659:'=':function as left operand

c++ sfml
1个回答
0
投票

answer2.getFillColor = answer;

是错的。 getFillColor是一个函数,你不能为它赋值。如果你想要该函数的值使用answer2.getFillColor()并让它在表达式的右侧。

answer = answer2.getFillColor();

假设你的代码,answer是一个std::string。但你无法将sf::Color转换为std::string。相反,让answer成为sf::Color。通过这种方式,您可以通过与sf::Color::Blue进行比较来检查它是否为蓝色:

sf::Color answer;
//...
answer = answer2.getFillColor();
if (answer == sf::Color::Blue)
{
    question.setString("Correct");
}
else
{
    question.setString("Wrong");
}
© www.soinside.com 2019 - 2024. All rights reserved.