Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/149.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 速度增加_C++_Sfml - Fatal编程技术网

C++ 速度增加

C++ 速度增加,c++,sfml,C++,Sfml,当我移动物体时,它开始非常缓慢,然后开始加速。我想要一个恒定的速度,但我不知道出了什么问题 const float m_Walkspeed = 0.1; if (sf::Keyboard::isKeyPressed(sf::Keyboard::Left) && m_position.x > 0) { m_velocity.x -= m_Walkspeed; CurrentAnimation = &Animat

当我移动物体时,它开始非常缓慢,然后开始加速。我想要一个恒定的速度,但我不知道出了什么问题

    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);
}
const float m_Walkspeed=0.1;
如果(sf::Keyboard::isKeyPressed(sf::Keyboard::Left)&&m_position.x>0)
{
m_速度。x-=m_步行速度;
CurrentAnimation=&AnimationLeft;
}
如果(sf::Keyboard::isKeyPressed(sf::Keyboard::Right)&&m_position.x<800)
{
m_速度。x+=m_步行速度;
CurrentAnimation=&AnimationRight;
}
如果(sf::Keyboard::isKeyPressed(sf::Keyboard::Up)&&m_position.y>0)
{
m_速度。y-=m_步行速度;
CurrentAnimation=&AnimationUp;
}
如果(sf::Keyboard::isKeyPressed(sf::Keyboard::Down)和m_位置y<560)
{
m_速度。y+=m_步行速度;
CurrentAnimation=&AnimationDown;
}
m_位置+=m_速度;
m_速度=sf::Vector2f(0,0);
当前动画->设置位置(m_位置);
}

<>代码> 假设你在每个帧调用这个代码,你必须考虑上次帧后的时间:


我想这是你的游戏更新功能。是否定期调用此函数?如果不是的话,你需要找到函数上次运行以来的时间,并适当调整位置变化。你的意思是说如果你按下一个键,速度会增加吗?
m_velocity += timeSinceLastFrame;
m_position += m_velocity;