C++ C++;SFML 2文本未按预期更新
我是SFML新手,我正在尝试制作简单的游戏,但我有一个问题: 我有一个文本类来处理我的分数,这里是.h文件C++ C++;SFML 2文本未按预期更新,c++,C++,我是SFML新手,我正在尝试制作简单的游戏,但我有一个问题: 我有一个文本类来处理我的分数,这里是.h文件 #pragma once #include <SFML\Graphics.hpp> #include <string> #include <iostream> class Text { public: Text(); Text(std::string, std::string, sf::Vector2f position, int); ~Text(
#pragma once
#include <SFML\Graphics.hpp>
#include <string>
#include <iostream>
class Text
{
public:
Text();
Text(std::string, std::string, sf::Vector2f position, int);
~Text();
void ModifyScoreBy(int value);
void Update();
void Draw(sf::RenderWindow &window);
static int getScore();
private:
sf::Text text;
sf::Font* font;
std::string pathToFont;
std::string textString;
static int numberScore;
};
#pragma一次
#包括
#包括
#包括
课文
{
公众:
Text();
文本(std::string,std::string,sf::Vector2f位置,int);
~Text();
void ModifyScoreBy(int值);
无效更新();
虚线绘制(sf::渲染窗口和窗口);
静态int getScore();
私人:
sf::文本;
sf::Font*Font;
std::stringpathtofont;
std::string textString;
静态整数核;
};
和.cpp文件:
#include "Text.h"
int Text::numberScore = 0;
Text::Text()
{
}
Text::Text(std::string pathToFolder, std::string Text, sf::Vector2f position, int numberScore)
{
font = new sf::Font;
if (!font->loadFromFile(pathToFolder))
system("pause");
textString = Text;
this->numberScore = numberScore;
textString += std::to_string(numberScore);
text.setString(textString);
text.setFont(*font);
text.setPosition(position);
}
Text::~Text()
{
}
int Text::getScore()
{
return numberScore;
}
void Text::ModifyScoreBy(int value)
{
numberScore += value;
}
void Text::Update()
{
std::cout <<"Score: " << numberScore << std::endl;
textString = "Score: " + std::to_string(numberScore);
}
void Text::Draw(sf::RenderWindow &window)
{
window.draw(text);
}
#包括“Text.h”
int Text::numberScore=0;
Text::Text()
{
}
Text::Text(std::string路径文件夹,std::string Text,sf::Vector2f位置,int numberScore)
{
font=新sf::font;
如果(!font->loadFromFile(pathToFolder))
系统(“暂停”);
Text字符串=文本;
此->numberScore=numberScore;
textString+=std::to_字符串(numberScore);
text.setString(textString);
text.setFont(*字体);
text.setPosition(位置);
}
Text::~Text()
{
}
int Text::getScore()
{
返回号码核心;
}
无效文本::ModifyScoreBy(int值)
{
numberScore+=值;
}
void Text::Update()
{
std::cout也许您应该在Update()的末尾添加text.setString(textString);
(就在修改textString
之后)
另外,numberScore
可能不应该是一个静态变量,font
永远不会被释放。在添加text.setString(textString)后仍然不工作;关于numberScore我还想知道它不应该是一个静态变量,但我不知道它为什么工作不好(当它不是静态的时候,在使用ModifyScoreBy之后)(对于ex.10)函数不是在之前的分数上加10,而是每次都将其设置为10),因此我将其设置为静态,并修复了问题。不过,感谢您的帮助!