使用对象数组制作 sfml 动画 [英] animation of sfml by using array of objects

查看:75
本文介绍了使用对象数组制作 sfml 动画的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How to animate sprite with this code?I know what i should to add time delay,but how do it? I use array of objects to they's fast changing. or is it an irrational way of animating?

#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);
        }
    }
}

推荐答案

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

As said in the comments. For the animation you need to capture the time. Or you will not be able to calculate the duration per frame. And you need to store the current frame in a variable. I put some comment in the example code below. It is not a class anymore because I think we need a running code block here.

另一种是精灵表,参见 https://www.gamefromscratch.com/post/2015/10/26/SFML-CPP-Tutorial-Spritesheets-and-Animation.aspx 我可以用精灵重写答案sheet,但我认为您想先了解动画的基本机制.请在对我的帖子的评论中指出这一点.

An alternative is the sprite sheet, see https://www.gamefromscratch.com/post/2015/10/26/SFML-CPP-Tutorial-Spritesheets-and-Animation.aspx I can re-write the answer with the sprite sheet, but I think you wanted to understand the basic mechanism of an animation first. Please indicate so in a comment to my post.

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

Btw, if you are not used to game loops, have a look on 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();
  }
}

这篇关于使用对象数组制作 sfml 动画的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆