【发布时间】:2019-05-21 22:27:09
【问题描述】:
我正在使用带有 C++ 的 SFML 来尝试移动应用了特定纹理的精灵,但它并没有像我想要的那样移动。
我是 SFML 新手,我在 sfml 网站上阅读了有关运动的教程,并在 Youtube 上观看了许多关于此的教程。我看到的东西要么非常基础,要么目前已经足够复杂(主要是游戏开发的东西)
#include <SFML/Graphics.hpp>
#include <thread>
#include <chrono>
#include<iostream>
using namespace sf;
using namespace std;
int main()
{
sf::RenderWindow window(sf::VideoMode(600, 600), "test!");
Texture t;
t.loadFromFile("/*texture directory*/");
Sprite s(t);
s.setScale(Vector2f(0.25f, 0.25f));
float temp = 0;
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(Color::White);
window.draw(s);
window.display();
s.move(Vector2f(temp, temp));
for (int i = 0; i < 3; i++) {
if (i == 0) {
this_thread::sleep_for(chrono::milliseconds(1000));
temp = 10;
}
else if (i == 1) {
this_thread::sleep_for(chrono::milliseconds(1000));
temp = 20;
}
else {
this_thread::sleep_for(chrono::milliseconds(1000));
temp = -10;
}
}
}
return 0;
}
我希望精灵移动到 (10,10),然后在一秒钟后,(20,20),然后在一秒钟后,移动回 (10,10)。 相反,精灵每三秒只移动一次 (-10,-10),所以它只在循环结束时获取 temp 的值。 setPosition 也是如此。
这里有一些非常基本的东西在起作用,但研究让我得到的问题比答案还要多,这就是我来这里的原因。
【问题讨论】: