0

私は 2 つのクラスを書きました。アニメーション、俳優。なぜかスプライト画像が変化しません。ずっと最初のフレームのままです。コードは問題ありません。コンパイルに問題はありません。ロジックがどこかで間違っていて、期待どおりに動作していません。

アニメーション クラスの宣言:

class Animation
{
public:
        Animation(std::string path, int frames);
        ~Animation();
        void nextFrame();
        void gotoStart();
        bool loadFrames();
        sf::Texture& getActiveFrame();

private:
        int frameCount;
        std::string pathToAnimation;
        int currentFrame;
        sf::Texture frame[];
};

アニメーション クラスの実装:

Animation::Animation(std::string path, int frames)
{
        pathToAnimation = path;
        frameCount = frames;
}

Animation::~Animation()
{
        // destructor
}

void Animation::nextFrame()
{
        if(currentFrame < frameCount)
        {
            currentFrame = 1;
        }
        else
            currentFrame += 1;
}

void Animation::gotoStart()
{
        currentFrame = 1;
}

bool Animation::loadFrames()
{
        for(int i = 01; i < frameCount; i++)
        {
            if(!frame[i].loadFromFile(pathToAnimation + std::to_string(i) + ".jpg")) return false;
        }
        return true;
}

sf::Texture& Animation::getActiveFrame()
{
    return frame[currentFrame];
}

アクター クラスの宣言:

class Actor
{
public:
        Actor();
        ~Actor();
        void setActiveAnimation(std::shared_ptr<MaJR::Animation> anim);
        void draw(sf::RenderWindow& win);

private:
        sf::Sprite sprite;
        std::shared_ptr<MaJR::Animation> activeAnimation;
};

アクター クラスの実装:

Actor::Actor()
{
    // constructor
}

Actor::~Actor()
{
    // destructor
}

void Actor::setActiveAnimation(std::shared_ptr<MaJR::Animation> anim)
{
    activeAnimation = anim;
    activeAnimation->gotoStart();
}

void Actor::draw(sf::RenderWindow& win)
{
    sprite.setTexture(activeAnimation->getActiveFrame());
    win.draw(sprite);
    activeAnimation->nextFrame();
}

テストするコードは次のとおりです。

int main()
{
    sf::RenderWindow Window(sf::VideoMode(800, 600), "MaJR Game Engine Sandbox");

    std::shared_ptr<MaJR::Animation> DroneStandNorth = std::make_shared<MaJR::Animation>("./Assets/Sprites/Zerg/Units/Drone/Stand/North/", 61);
    if(!DroneStandNorth->loadFrames()) return EXIT_FAILURE;
    MaJR::Actor Drone;
    Drone.setActiveAnimation(DroneStandNorth);

    while(Window.isOpen())
    {
        sf::Event Event;
        while(Window.pollEvent(Event))
        {
            if(Event.type == sf::Event::Closed)
                Window.close();
        }

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
            Window.close();

        Window.clear();
        Drone.draw(Window);
        Window.display();
    }

    return 0;
}

ここで何が問題なのか、私は完全に途方に暮れています。すべてを自分でコンパイルしたい場合は、元のファイルを次に示します: http://www.filedropper.com/animationtesttar -std=c++0x を使用するか、コンパイラで C++11 を使用するために必要なこと.

4

1 に答える 1

1

アニメーションの実装には、次のものがあります。

void Animation::nextFrame()
{
    if(currentFrame < frameCount)
    {
        currentFrame = 1;
    }
    else
        currentFrame += 1;
}

currentFrame は 1 から始まるため、常に frameCount よりも小さいため、常に 1 に設定されます。次のように変更します。

void Animation::nextFrame()
{
    if(currentFrame >= frameCount)
    {
        currentFrame = 1;
    }
    else
        currentFrame += 1;
}

このように、currentFrame が frameCount と等しい場合 (テスト ケース 61)、リセットされます。nextFrame() はアクタの Draw() の後に呼び出されるため、最後のフレームが描画され、次に currentFrame が 1 にリセットされます。

于 2012-07-03T03:04:33.810 に答える