有什么方法可以将我所有的文本或一些指定的文本全部设置为相同的字体、颜色等?
这样做会更快更容易,而不是这样做:
text1.setColor(sf::Color::Black);
text2.setColor(sf::Color::Black);
text3.setColor(sf::Color::Black);
...
我在 C++98 中使用 SFML 2.1。
最佳答案
如果 Text
实例如此相似,将它们保存在 std::vector
或其他一些容器类中可能是有意义的。如果它们可以合理地放置在这样的容器中,您可以简单地遍历它们并更改您想要的任何属性:
for (std::vector<sf::Text>::iterator it = myvector.begin() ; it != myvector.end(); ++it)
{
it->setColor(sf::Color::Black);
it->setFont(myfont);
}
在评论中编辑对 C++11 的兴趣:
在 C++11 中,由于 automatic type deduction,这变得更加简单和 range-based for loops .上面的语法简化为:
for (auto& text : myvector) //don't miss the & or 'text' will be read-only!
{
text.setColor(sf::Color::Black);
text.setFont(myfont);
}
关于c++ - SFML 2.1 设置所有文本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25904000/