【发布时间】:2017-06-08 10:51:17
【问题描述】:
我正在尝试制作一个程序,以便它显示一个头骨的图像,它的嘴张开,然后以 1 秒的间隔关闭。当我运行它时,它只显示“skullmouthopen”。我不确定我哪里出错了,我没有收到任何错误消息。只是给我朋友的一个小病毒恶作剧:)
#include <SFML/Graphics.hpp>
#include <iostream>
#include <chrono>
#include <thread>
using namespace sf;
void f()
{
std::this_thread::sleep_for(std::chrono::seconds(1));
}
int main()
{
RenderWindow gameDisplay(VideoMode(800, 600), "Oops");
while (gameDisplay.isOpen())
{
Event event;
while (gameDisplay.pollEvent(event))
{
if (event.type == Event::Closed)
gameDisplay.close();
}
Texture texture;
if (!texture.loadFromFile("o_cdf78ec6b8037e00-0.png"))
{
// error...
}
Texture texture2;
if (!texture.loadFromFile("o_cdf78ec6b8037e00-1.png"))
{
// error...
}
sf::Sprite skullmouthclosed;
skullmouthclosed.setTexture(texture);
skullmouthclosed.setPosition(300, 200);
Sprite skullmouthopen;
skullmouthopen.setTexture(texture2);
skullmouthopen.setPosition(300, 200);
gameDisplay.draw(skullmouthclosed);
gameDisplay.display();
f();
gameDisplay.draw(skullmouthopen);
gameDisplay.display();
f();
}
return 0;
}
【问题讨论】:
-
你在同一个纹理上调用了两次
loadFromFile。此外,该代码不应在循环中,并且您不需要两个精灵。 投票结束。 -
如果图像有透明部分,或者您打算稍后移动它们,您会错过
gameDisplay.clear();。
标签: c++ image animation textures sfml