C++ 雷神:只播放一帧的动画师

C++ 雷神:只播放一帧的动画师,c++,animation,sfml,thor,C++,Animation,Sfml,Thor,我一直在尝试用Thor 2.1制作sf::Sprite的动画,但到目前为止,我的Sprite只显示每个动画的一帧,而不是全部 我从SFML的精灵表教程中获得了一个精灵表示例: 我的代码(原理图代码,不是全部): #包括 #包括 #包括 #包括 #包括 #包括 #包括 #包括 int main() { sf::纹理特克斯播放器; texPlayer.create(96128); 如果(!texPlayer.loadFromFile(workingDirectory+“\\textures\\an

我一直在尝试用Thor 2.1制作sf::Sprite的动画,但到目前为止,我的Sprite只显示每个动画的一帧,而不是全部

我从SFML的精灵表教程中获得了一个精灵表示例:

我的代码(原理图代码,不是全部):

#包括
#包括
#包括
#包括
#包括
#包括
#包括
#包括
int main()
{
sf::纹理特克斯播放器;
texPlayer.create(96128);
如果(!texPlayer.loadFromFile(workingDirectory+“\\textures\\animtest.png”))
{
std::cout首先

不需要,因为您会立即加载文件,从而覆盖纹理。
sf::texture::create()
主要用于加载纹理后编辑纹理,或用于低级OpenGL

thor::Animator可以理解为有两种操作模式,每种模式都包含一些方法:

  • 选择/切换动画:
    播放()
    停止()
    队列()

    仅当要更改动画播放(角色开始移动/攻击)或要停止播放动画(角色静止)时,才应调用此选项
  • 更新状态并应用图形:
    Update()
    animate()

    这应该应用于每一个图形帧。如果您想要确定的动画行为(您的动画需要不断的进度),您应该在固定的时间逻辑步骤中调用
    update()
这些已经是
thor::Animator
提供的所有方法,请参阅

代码中的错误是在每一帧中调用
play()
,从而不断重新启动动画。您应该做的是在
while
循环之前调用
play()
。由于您指定了
thor::Playback::loop()
,只要更新动画制作者,动画就会循环

#include<SFML/Window.hpp>
#include<SFML/Audio.hpp>
#include<SFML/Graphics.hpp>
#include<SFML/System.hpp>
#include<SFML/Main.hpp>
#include<stdlib.h>
#include<iostream>
#include <Thor/Animations.hpp>

int main()
{

sf::Texture texPlayer;
texPlayer.create(96, 128);
if (!texPlayer.loadFromFile(workingDirectory + "\\textures\\animtest.png"))
{
    std::cout << "can't load" << std::endl;
}

sf::Sprite sprPlayer;
sprPlayer.setTexture(texPlayer);

thor::FrameAnimation playerDown;
playerDown.addFrame(10.0f, sf::IntRect(0 , 0, 32, 32));
playerDown.addFrame(10.0f, sf::IntRect(32, 0, 32, 32));
playerDown.addFrame(10.0f, sf::IntRect(64, 0, 32, 32));

thor::FrameAnimation playerLeft;
playerLeft.addFrame(20.0f, sf::IntRect(0 , 32, 32, 32));
playerLeft.addFrame(20.0f, sf::IntRect(32, 32, 32, 32));
playerLeft.addFrame(20.0f, sf::IntRect(64, 32, 32, 32));

thor::FrameAnimation playerRight;
playerRight.addFrame(30.0f, sf::IntRect(0 , 64, 32, 32));
playerRight.addFrame(30.0f, sf::IntRect(32, 64, 32, 32));
playerRight.addFrame(30.0f, sf::IntRect(64, 64, 32, 32));

thor::FrameAnimation playerUp;
playerUp.addFrame(40.0f, sf::IntRect(0 , 96, 32, 32));
playerUp.addFrame(40.0f, sf::IntRect(32, 96, 32, 32));
playerUp.addFrame(40.0f, sf::IntRect(64, 96, 32, 32));

thor::AnimationMap<sf::Sprite, std::string> playerAMap;
playerAMap.addAnimation("down" , playerDown , sf::seconds(2.0f));
playerAMap.addAnimation("left" , playerLeft , sf::seconds(3.0f));
playerAMap.addAnimation("right", playerRight, sf::seconds(4.0f));
playerAMap.addAnimation("up"   , playerUp   , sf::seconds(5.0f));

thor::Animator<sf::Sprite, std::string> playerAnimator = thor::Animator<sf::Sprite, 
std::string>::Animator(playerAMap);

sf::Clock animFrameTime;

sf::RenderWindow window;
window.create(sf::VideoMode(800, 450), "Placeholder", sf::Style::Default);

while (window.isOpen())
{

// here's movement direction checking, event handling, dealing with movement overall

// let's say we dont use any of that now and we want to play just the "up" animation

playerAnimator.play() << "up" << thor::Playback::loop("up");

playerAnimator.update(animFrameTime.restart());
playerAnimator.animate(sprPlayer);

window.draw(sprPlayer);
}

return 0;
}
texPlayer.create(96, 128);