【发布时间】:2015-09-13 17:49:20
【问题描述】:
tl/dr:我已将函数调用从类内部移至类外部,但该函数停止工作。
我遇到了一年中最令人困惑的问题,所以我一直在使用 c++。我找不到任何东西来解释这里发生了什么,但老实说,我什至很难提出一个 SEO 问题。
这里的基础操作比较简单,
- 创建一个 sf::Texture 和 sf::Sprite 对象
- 将纹理加载到 sf::Texture 对象
- 将 sf::Sprite 的纹理设置为纹理对象
- 显示 sf::Sprite
在一个函数中所有 4 个步骤都很好,但因为我的目标是构建一个游戏引擎,我开始将它封装到更大的类中。
我创建了一个 GameEngine 类并让它处理第 4 步。经过一些更正后一切顺利。
然后我创建了一个 GameObject 类来处理前三个步骤,作为框架的“用户”,我所要做的就是创建对象并告诉它进行渲染,这也有效。
然后,当我将第 2 步的函数调用从对象的构造函数移到对象外部时,我遇到了障碍。
旧情况: 类游戏对象 { 对象类型 d_type; 游戏引擎 *d_engine;
sf::Texture d_texture;
sf::Sprite d_sprite;
bool isactive;
public:
GameObject(GameEngine *engine, ObjectType type);
void addtexture(std::string textpath);
void render();
和
GameObject::GameObject(GameEngine *engine, ObjectType type)
:
d_type(type),
d_engine(engine),
d_texture(),
d_sprite(),
isactive{false}
{
addtexture("textures//MenuBackGround.png"); //<- problematic line
d_sprite.setTexture(d_texture);
}
void GameObject::addtexture(std::string textpath)
{
if(!d_texture.loadFromFile(textpath))
{
cout << "couldn't load texture in\n";
} else
{
cout << "did load texture\n";
}
}
这行得通,我看到我创建的纹理出现在窗口中。如果我现在创建一个类 Loadingscreen:
class Loading_Screen : public Base_State
{
std::vector<GameObject> d_backgrounds;
public:
Loading_Screen(GameEngine *engine);
virtual ~Loading_Screen();
实施:
Loading_Screen::Loading_Screen(GameEngine *engine)
{
GameObject temp(engine, ObjectType::BACKGROUND);
d_backgrounds.push_back(temp);
temp.addtexture("textures//MenuBackGround.png");
}
我只看到黑屏。但在这两种情况下,我都会收到纹理已加载的消息。
【问题讨论】: