当我移动物体时,它的启动速度非常慢,然后开始加速。我想要一个恒定的速度,但我不知道出了什么问题
const float m_Walkspeed = 0.1;
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && m_position.x > 0)
{
m_velocity.x -= m_Walkspeed;
CurrentAnimation = &AnimationLeft;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Right) && m_position.x < 800)
{
m_velocity.x += m_Walkspeed;
CurrentAnimation = &AnimationRight;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up) && m_position.y > 0)
{
m_velocity.y -= m_Walkspeed;
CurrentAnimation = &AnimationUp;
}
if (sf::Keyboard::isKeyPressed(sf::Keyboard::Down) && m_position.y < 560)
{
m_velocity.y += m_Walkspeed;
CurrentAnimation = &AnimationDown;
}
m_position += m_velocity;
m_velocity = sf::Vector2f(0, 0);
CurrentAnimation->setPosition(m_position);
}
答案 0 :(得分:1)
假设您在每一帧调用此代码,您必须考虑自上一帧以来的时间:
m_velocity += timeSinceLastFrame;
m_position += m_velocity;