【发布时间】:2015-08-31 04:25:58
【问题描述】:
我遇到了一个奇怪的问题,一个精灵正在加载,而另一个精灵却没有
这是我的 main.cpp
window.draw(universe.getPlayer()->draw()); //Draw Player
std::list<AbstractBlock*>::const_iterator i;
std::list<AbstractBlock*>* values = universe.getLoadedBlocks();
for (i = values->begin(); i != values->end(); ++i){
window.draw((*i)->draw()); //Draw Blocks
}
window.display();
在这里你可以看到玩家绘图和宇宙中的所有方块绘图。但是,只有玩家绘制并且块根本不绘制。我已经确保循环确实有效。但是,因为 draw() 返回 void 我实际上无法查看它是否有效。
这里是 DirtBlock.cpp(我继承自 AbstractBlock)
DirtBlock::DirtBlock(int x, int y, float rotation, b2World *world){
bodyDef.position.Set(x, y);
bodyDef.linearDamping = .03f;
bodyDef.type = b2_dynamicBody;
fixDef.density = .1f;
b2PolygonShape shape;
shape.SetAsBox(16, 16);
fixDef.shape = &shape;
body = world->CreateBody(&bodyDef);
body->CreateFixture(&fixDef);
texture.loadFromFile("Dirt.bmp");
sprite.setTexture(texture);
sprite.setOrigin(16, 16);
}
sf::Sprite DirtBlock::draw(){
sprite.setPosition(body->GetPosition().x, body->GetPosition().y);
return sprite;
}
并非所有内容都包括在内,仅包括与绘图有关的内容。
我的 Player 类非常相似:
Player::Player(b2World *world){
texture.loadFromFile("player.bmp");
bodyDef.position.Set(10, 10);
bodyDef.type = b2_dynamicBody;
fixDef.density = .1f;
b2PolygonShape shape;
shape.SetAsBox(16, 16);
fixDef.shape = &shape;
body = world->CreateBody(&bodyDef);
body->CreateFixture(&fixDef);
body->SetLinearDamping(.03f);
sprite.setTexture(texture);
sprite.setOrigin(16, 16);
force = 10.f;
}
sf::Sprite Player::draw(){
sprite.setPosition(body->GetPosition().x, body->GetPosition().y);
sprite.setRotation(body->GetAngle() * (180 / b2_pi));
return sprite;
}
既然它们如此相似,为什么一幅画而另一幅不是?我有一种感觉,这可能是因为我的继承。我通常是一名 Java 程序员,我不能 100% 确定我在 C++ 中正确地进行了继承。应该是这样吗? (我的 DirtBlock.h)
class DirtBlock: public AbstractBlock
{
public:
DirtBlock();
DirtBlock(int x, int y, float rotation, b2World *world);
~DirtBlock();
virtual sf::Sprite draw();
virtual void destroy(b2World *world);
private:
sf::Sprite sprite;
};
【问题讨论】:
-
您的纹理是否可能加载不正确?