【发布时间】:2019-07-23 23:40:11
【问题描述】:
我是 C++ 和 SFML 的新手。我正在尝试使用循环使我的精灵对象相对于其最后一个位置向下移动。我正在寻找程序启动时它的精灵对象掉落的动画。
我认为在我的 for 循环中实现 sleep 函数将有助于解决我遇到的问题,即程序只会在循环的最后一次迭代中显示对象。但是我的程序只是冻结和崩溃。
寻找方向。也许 sleep 函数在这里调用不正确?
#include <SFML/Graphics.hpp>
#include <Windows.h>
#include <iostream>
using namespace std;
int main()
{
// Create the window here. Calling out the dimensions
sf::RenderWindow window(sf::VideoMode(800, 600), "Example Window");
// run the program as long as the window is open
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
//close window we requested
if (event.type == sf::Event::Closed)
{
window.close();
}
}
window.clear(sf::Color::Black);
sf::Texture texture;
if (!texture.loadFromFile("c:\\abstract.png"))
{
cout<<"Failed to load image...";
}
sf::Sprite sprite;
sprite.setTexture(texture);
sprite.setTextureRect(sf::IntRect(20,20,30,30));
for (float i = 0; i < 30.; i++)
{
sprite.move(sf::Vector2f(5.f, i));
window.draw(sprite);
Sleep(50);
}
window.display();
}
return 0;
}
【问题讨论】: