【问题标题】:animation of sfml by using array of objects使用对象数组的 sfml 动画
【发布时间】:2018-10-18 22:25:52
【问题描述】:

如何使用这段代码为精灵设置动画?我知道我应该添加什么时间延迟,但是怎么做呢?我使用对象数组来快速变化。还是一种不合理的动画方式?

#include <SFML/Graphics.hpp>
#include <iostream>
using namespace std;
sf::RenderWindow window(sf::VideoMode(600, 400), "!!!");
void animation()
{
    sf::Texture arrayOfTexture[9];
    sf::Sprite imageOfLamp;
    arrayOfTexture[0].loadFromFile("1.png");
    arrayOfTexture[1].loadFromFile("2.png");
    arrayOfTexture[2].loadFromFile("3.png");
    arrayOfTexture[3].loadFromFile("4.png");
    arrayOfTexture[4].loadFromFile("5.png");
    arrayOfTexture[5].loadFromFile("6.png");
    arrayOfTexture[6].loadFromFile("7.png");
    arrayOfTexture[7].loadFromFile("8.png");
    arrayOfTexture[8].loadFromFile("9.png");
    for(;;)
    {
        for(int i= 0;i <=8;i++)
        {
            imageOfLamp.setTexture(arrayOfTexture[i]);
            window.draw(imageOfLamp);
        }
        for(int i =8;i >=0;i--)
        {
            imageOfLamp.setTexture(arrayOfTexture[i]);
            window.draw(imageOfLamp);
        }
    }
}

【问题讨论】:

  • 感觉就像你跳过了一两节课。您应该预先/后台加载图像,打开磁盘上的文件需要大量时间。您应该查看如何使用sf::clock/sf::time 管理时间并利用这些来决定应该在当前帧上渲染哪个灯,通常当我们发现自己使用无限循环时,我们做错了。跨度>
  • 您还应该尝试将动画的所有纹理组合在一个文件中,因此您只需切换texture rectangle

标签: c++ sfml


【解决方案1】:

正如 cmets 中所说。对于动画,您需要捕捉时间。否则您将无法计算每帧的持续时间。您需要将当前帧存储在一个变量中。我在下面的示例代码中添加了一些注释。它不再是一个类,因为我认为我们需要在这里运行代码块。

另一种方法是精灵表,见https://www.gamefromscratch.com/post/2015/10/26/SFML-CPP-Tutorial-Spritesheets-and-Animation.aspx 我可以用精灵表重写答案,但我想你想先了解动画的基本机制。请在我的帖子的评论中注明。

顺便说一句,如果您不习惯游戏循环,请查看https://gafferongames.com/post/fix_your_timestep/

#include <SFML/Graphics.hpp>

int main(){
  sf::RenderWindow renderWindow(sf::VideoMode(800, 600), "Color Animation");

  // Duration to control animation speed
  int currentFrame = 1;
  float duration = float();

  sf::Clock clock;
  sf::Event event;

  sf::Texture arrayOfTexture[9];
  arrayOfTexture[0].loadFromFile("1.png");
  arrayOfTexture[1].loadFromFile("2.png");
  arrayOfTexture[2].loadFromFile("3.png");
  arrayOfTexture[3].loadFromFile("4.png");
  arrayOfTexture[4].loadFromFile("5.png");
  arrayOfTexture[5].loadFromFile("6.png");
  arrayOfTexture[6].loadFromFile("7.png");
  arrayOfTexture[7].loadFromFile("8.png");
  arrayOfTexture[8].loadFromFile("9.png");

  // Assign basic texture to sprite
  sf::Sprite imageOfLamp;
  imageOfLamp.setTexture(arrayOfTexture[0]);

  while (renderWindow.isOpen()){
    // How much time since last loop?
    sf::Time dt = clock.restart();
    duration += dt.asSeconds();

    while (renderWindow.pollEvent(event)){
      //Handle events here
      if (event.type == sf::Event::EventType::Closed)
        renderWindow.close();
    }

    // Animation duration per frame (0.1f) reached
    if (duration > 0.1f){
      // Restart calculation of the duration
      duration = 0;

      // Loop through the animation frames
      if (currentFrame < 9){
        currentFrame += 1;
      } else {
        // Start from first frame if last frame reached
        currentFrame = 0;
      }
      imageOfLamp.setTexture(arrayOfTexture[currentFrame]);
    }

    // Clear render window and draw sprite
    renderWindow.clear(sf::Color::Black);
    renderWindow.draw(imageOfLamp);
    renderWindow.display();
  }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-11-18
    • 2014-06-11
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多