动画类声明:
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::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。
答案 0 :(得分: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中)时,它将被重置。由于在Actor的Draw()之后调用nextFrame(),因此将绘制最后一帧,然后将currentFrame重置为1.