首页 > 解决方案 > How to store a series of SFML sprites

问题描述

I am learning C++ at the moment and when I start learning a language I like to start with a small game to get used to Object Oriented programming in the language so don't question my project choice.

So, I am using SFML 2.5, and I am making an Animation manager, I already have the animation that works fine. But the manager that would basically only allow one animation to be drew at once and that sort of thing isn't working fully. The manager itself works fine, but there is a bug which I don't know how to fix.

Here is the AnimationManager.h:

#pragma once

#include <memory>

#include "animation.h"
#include <SFML/Graphics.hpp>

class AnimationManager {
    public:
        AnimationManager() = default;

        void runAnimation(float);
        void setAnimation(Animation*);
        void swapAnimation(Animation*, float);
        void draw(sf::RenderWindow&);
        void tick();

        void setScale(float, float);
        void setRotation(float);
        void setPosition(float, float);
        void setOrigin(int, int);

    private:
        Animation* currentAnimation;
};

Then my AnimationManager.cpp:

#include "../headers/animationManager.h"

void AnimationManager::setAnimation(Animation* animation) { AnimationManager::currentAnimation = animation; }
void AnimationManager::swapAnimation(Animation* animation, float dt) {
    if(animation->id != AnimationManager::currentAnimation->id) {
        AnimationManager::runAnimation(dt);
        AnimationManager::currentAnimation = animation;
    }
}

void AnimationManager::draw(sf::RenderWindow& g) { AnimationManager::currentAnimation->draw(g); }
void AnimationManager::tick() { AnimationManager::currentAnimation->tick(); }
void AnimationManager::runAnimation(float dt) { AnimationManager::currentAnimation->runAnimation(dt); }

void AnimationManager::setOrigin(int x, int y) { AnimationManager::currentAnimation->setOrigin(x, y); }
void AnimationManager::setPosition(float x, float y) { AnimationManager::currentAnimation->setPosition(x, 
y); }
void AnimationManager::setRotation(float r) { AnimationManager::currentAnimation->rotate(r); }
void AnimationManager::setScale(float x, float y) { AnimationManager::currentAnimation->scale(x, y); }

My problem is that I can't draw two animations at the same time, because I will have multiple enemies with the same animation sprite I want to be able to draw multiple at a time. I'm pretty sure this an issue with what way I'm storing the animation, I have tried using a reference but it had the same effect.

By the way the Animation class isn't needed because before the AnimationManager I just drew the animation by itself and that worked fine.

Here is a recording of what I'm talking about, the size of the sprites doesn't matter since I just havent changed the size for all of the sprites.

enter image description here

Notice when the player goes down the animation dissapears, this is because the other entity is also using the animation and starting using it first.

So my question is what way should I store the animation or is there something else I'm looking over?

标签: c++sfml

解决方案


推荐阅读