c++ - 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.
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?
解决方案
推荐阅读
- reactjs - 使用相对路径服务 React 应用程序
- c# - 如何查看索引的位置。喜欢 1-15 不是坐标
- amazon-ec2 - 如何检查 AWS EC2 AMI 是否需要 EULA 选择加入?
- python - 如何减少冒泡排序中的变量数量?
- javascript - 在推送到数组之前检查数组中是否存在项目
- google-apps-script - 在 People API 迁移之后,Google Apps 脚本联系人服务 (ContactsApp) 是否仍然有效?
- javascript - 为什么gdb调试器不能识别a.exe以外的exe文件
- react-native - 如何循环反应本机 useState 数组?
- node.js - 提交 Express 后端发布请求以添加到 Firebase Cloud Firestore 的 React 按钮
- julia - 如何以特定格式在 Julia 中保存数组或矩阵?