0
我想製作一個程序,以便它顯示一張頭顱的圖像,然後以1秒的間隔關閉它。當我運行它時,它只顯示「skullmouthopen」。我不確定我出錯的地方,我沒有收到任何錯誤消息。只是一個小小的病毒惡作劇爲我的朋友:)C++ SFML顯示多個圖像
#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'兩次。此外,該代碼不應該在循環中,你不需要兩個精靈。 **投票結束**。 – LogicStuff
如果圖像具有透明部分,或者您打算稍後移動它們,則缺少'gameDisplay.clear();'。 – LogicStuff