使用对象数组对 SFML 进行动画处理
animation of sfml by using array of objects
如何使用此代码为精灵制作动画?我知道我应该添加延时什么,但是怎么做呢?我使用对象数组来快速变化。还是一种非理性的动画方式?
#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);
}
}
}
正如评论中所说。对于动画,您需要捕获时间。否则,您将无法计算每帧的持续时间。并且您需要将当前帧存储在变量中。我在下面的示例代码中做了一些注释。它不再是一个类,因为我认为我们在这里需要一个运行的代码块。
另一种选择是精灵表,请参阅 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();
}
}
相关文章:
- 警告处理为错误这里有什么问题
- 在执行其他功能的同时播放动画(LED矩阵和Arduino/ESP8266)
- 在C#中处理C++指针而不使用unsafe的最佳方法
- 处理多个异常集合的C++方法
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 使用流处理接收到的数据
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- 基于多个条件处理地图中的所有元素
- 如何用数字处理log(0)
- 使用对象数组对 SFML 进行动画处理
- 围绕特定轴对 qt3d 旋转进行动画处理
- 是否可以在QT GUI应用程序中处理事件时播放加载动画指示器?
- 学习如何制作可以处理动画的程序的最佳方式
- QTableview单元格的值更新后,如何及时对它的颜色进行动画处理?
- 如何在 qt 中对 QPushButton 的背景颜色进行动画处理
- 无法在 qgraphicsItem 中对 QTransform 进行动画处理
- 对子类对象的数组进行动画处理
- QML如何对属性的每次更改进行动画处理?(只有最后一个更改动画可见)
- 如何在OpenGL中处理包含多个对象的动画