【问题标题】:Sprite not animating精灵没有动画
【发布时间】:2012-06-13 10:49:16
【问题描述】:

我写了两个类;动画,演员。由于某种原因,精灵图像不会改变。它始终保持第一帧。代码很好,我在编译时没有问题,我的逻辑在某个地方有问题,它没有按照我的预期行事。

动画类声明:

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];
}

Actor 类声明:

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::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。

【问题讨论】:

    标签: animation sprite sfml


    【解决方案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() 在 Actor 的 Draw() 之后被调用,最后一帧将被绘制,然后 currentFrame 将被重置回 1。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-12-01
      • 2010-12-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多