我写了两节课;动画,演员。由于某种原因,精灵图像不会改变。它始终保持第一帧。代码很好,我编译没有问题,我的逻辑在某个地方是错误的,它没有按照我的预期行事。
动画类声明:
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 .