我的 SFML/C++ 程序按预期工作,但如何使移动变慢
My SFML/C++ program is working as expected but how to make movement slow?
我作为我们的玩家做了一个圆圈并设置了重力,但它的工作速度要快得多,我什至看不到。我认为它必须与 sf::time 或 sf::clock 做一些事情,但是如何做呢?我实际上没有得到这两个,所以任何代码示例和解释将不胜感激。这是我的代码:-
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
sf::CircleShape shape(20);
shape.setFillColor(sf::Color::White);
shape.setPosition(380, 550);
shape.setOutlineColor(sf::Color::Green);
shape.setOutlineThickness(3);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
shape.move(0, -100);
}
if(event.KeyReleased)
{
if(event.key.code == sf::Keyboard::Up)
{
int x[] = { 10, 10, 10, 10, 10};
for (int y = 0; y <= 4; y++)
{
shape.move(0, x[y]);
}
}
}
}
window.clear(sf::Color::Black);
window.draw(shape);
window.display();
}
return 0;
}
请记住,
您的程序可能每秒运行数百帧,因此当您按下键盘上的向上按钮时,它会移动对象-每帧 100 像素,这绝对不是您想要的。
那么如何解决这个问题呢?你需要包括时间的概念。为此,我们使用时间步长来查看自调用最后一帧以来已经过去了多少时间。
这是一个简短的示例代码,显示了如何构建此时间步(有许多不同的方法,每种方法都有优点和缺点)。
sf::Time timePerFrame = sf::seconds(1.0f / 60.0f); // 60 frames per second
sf::Clock deltaClock; // This will track how much time has past since the last frame
sf::Time timeSinceLastUpdate = sf::Time::Zero;
while (m_Window.isOpen())
{
sf::Time deltaTime = deltaClock.restart(); // Restart returns the time since the last restart call
timeSinceLastUpdate += deltaTime;
while (timeSinceLastUpdate >= timePerFrame)
{
timeSinceLastUpdate -= timePerFrame;
ProcessInput();
Update(timePerFrame); // Notice how I pass the time per frame to the update function
}
Render();
}
然后在更新代码中,您只需将要移动的距离乘以传递到更新函数中的对象 timePerFrame(确保使用 sf::Time::asSeconds()
)。例如,您的代码如下所示
shape.move(0, -100 * timePerFrame.asSeconds());
如果您正在寻找有关时间步长如何工作的深入解释,我会推荐这篇文章 http://gafferongames.com/game-physics/fix-your-timestep/
正如@Zereo所回应的那样,但有一个简化的版本:与其照顾帧率,不如让 SFML 照顾:
window.setFramerateLimit(60);
在创建窗口后放置此行,SFML 会将您的帧限制为 60/s。
相关文章:
- 使lambda不可复制/不可移动
- 我应该使我的局部变量常量还是可移动的
- 为什么字符串流只读取整数中的第一个数字?如何使它只在空白处移动?
- 移动构造函数是否使shared_from_this无效
- 为什么移动对象不会使其为空?
- 我可以在移动平台上有效地使用用C/C 编写的数字算法
- 您如何使形状沿着更BE的曲线SFML移动
- 我可以在 std::list 中移动元素而不会使迭代器或引用无效,但是如何移动呢?
- 改变对象的原点点使其以不同的轴移动
- 如何使角色在C 中移动
- 什么是使类不可移动的用例(如果有的话)
- 是否允许复制/移动省略使使用已删除函数的程序格式正确?
- 移动std::shared_ptr会使程序崩溃
- 我的 SFML/C++ 程序按预期工作,但如何使移动变慢
- 使对象在 C++-SFML 中平滑移动
- 使投射物移动与时间相关SFML C++
- 平台独立性:它与将源代码从一个操作系统移动到另一个操作系统有何不同
- 使用 CSplitterWindowImpl 处理移动并使子窗格无效
- 如何使移动检测到的手和指尖像鼠标一样与Qt GUI交互
- C++11编译器何时会使RVO和NRVO优于移动语义和常量引用绑定