class 中的 C++ SFML 纹理和精灵(白框)
C++ SFML texture and sprite in class (white box)
所以我正在寻找大约 1 个月的答案,没有人无法弄清楚发生了什么。我有 class 游戏对象,里面有精灵和纹理,当我调用函数 ->getSprite() 并将其绘制在我的 window 上时,我只得到白色框。我再次重新输入了我的代码 3 次,我一次又一次地遇到同样的问题。如果有人帮助我,我会很高兴。这是我的文件。 files
我曾经遇到过同样的问题。
我在加载纹理 (.png) 时遇到了困难,但我创建了一个函数,您只需为其提供源(作为字符串),然后 returns 有效的 sf::Sprite.
//loads a graphic from a source
static sf::Sprite loadSprite(std::string source){
static sf::Texture t;
if (!t.loadFromFile(source)){
std::cerr << "Error: File couldn't be loaded" << std::endl;
}
static sf::Sprite s;
s.setTexture(t);
return s;
}
尝试为 GameObject.h 提供此功能(可能还有其他文件)。
要使用它,只需添加一个
void setSprite(std::string source){
sprite = loadSprite(source); //call that function
}
函数到您的 GameObject.h public 成员函数之一,您也可以从构造函数调用。
试试看!祝你好运
所以我正在寻找大约 1 个月的答案,没有人无法弄清楚发生了什么。我有 class 游戏对象,里面有精灵和纹理,当我调用函数 ->getSprite() 并将其绘制在我的 window 上时,我只得到白色框。我再次重新输入了我的代码 3 次,我一次又一次地遇到同样的问题。如果有人帮助我,我会很高兴。这是我的文件。 files
我曾经遇到过同样的问题。 我在加载纹理 (.png) 时遇到了困难,但我创建了一个函数,您只需为其提供源(作为字符串),然后 returns 有效的 sf::Sprite.
//loads a graphic from a source
static sf::Sprite loadSprite(std::string source){
static sf::Texture t;
if (!t.loadFromFile(source)){
std::cerr << "Error: File couldn't be loaded" << std::endl;
}
static sf::Sprite s;
s.setTexture(t);
return s;
}
尝试为 GameObject.h 提供此功能(可能还有其他文件)。 要使用它,只需添加一个
void setSprite(std::string source){
sprite = loadSprite(source); //call that function
}
函数到您的 GameObject.h public 成员函数之一,您也可以从构造函数调用。 试试看!祝你好运