基本的には、スプライトのアクティブなアニメーションへの参照を、Actorクラスのプライベートメンバとして保存します。参照を使用してアニメーションを複数回作成する必要はありませんが、エラーが発生します。初期化されていない参照メンバ
Actorクラスの宣言:
class Actor
{
public:
Actor();
~Actor();
void setActiveAnimation(Animation anim);
void draw(sf::RenderWindow& win);
private:
sf::Sprite sprite;
MaJR::Animation& activeAnimation;
};
俳優クラスの実装:
Actor::Actor()
{
// constructor
}
Actor::~Actor()
{
// destructor
}
void Actor::setActiveAnimation(Animation anim)
{
activeAnimation = anim;
activeAnimation.gotoStart();
}
void Actor::draw(sf::RenderWindow& win)
{
sprite.setTexture(activeAnimation.getActiveFrame());
win.draw(sprite);
activeAnimation.nextFrame();
}
ビルド出力:
/home/mike/MaJR Game Engine/src/Actor.cpp||In constructor 'MaJR::Actor::Actor()':|
/home/mike/MaJR Game Engine/src/Actor.cpp|8|error: uninitialized reference member 'MaJR::Actor::activeAnimation' [-fpermissive]|
||=== Build finished: 1 errors, 0 warnings ===|
あなたの分析は良いですが、私は参照渡しのパラメータのアドレスを格納するコードで答えをupvoteできません。後で使用するためにポインタを格納する場合は、ポインタパラメータを受け入れて、呼び出し元にライフタイムの考慮事項を認識させます。 –
@Ben Voigt:私のコードで_ _ポインタを使用しないでください。 –
生ポインタがスマートポインタほど良くないことに同意します。しかし、参照ははるかに悪いです。 +1の修正。 –