Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/156.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++ SFML精灵移动未正确更新_C++_Sfml - Fatal编程技术网

C++ SFML精灵移动未正确更新

C++ SFML精灵移动未正确更新,c++,sfml,C++,Sfml,我正在创建一个简单的游戏,使用游戏状态。 我有一个调用running()方法的gameLoop()方法,该方法包含正确工作的切换状态。 这是gameLoop()方法: void Game::gameLoop() { while (window.isOpen()) { Event event; while (window.pollEvent(event)) { if (event.type == Event::Closed){ window.clos

我正在创建一个简单的游戏,使用游戏状态。 我有一个调用
running()
方法的
gameLoop()
方法,该方法包含正确工作的切换状态。 这是
gameLoop()
方法:

void Game::gameLoop()
{
  while (window.isOpen()) {
    Event event;
    while (window.pollEvent(event)) {
      if (event.type == Event::Closed){
        window.close();
      }
    }
    if (Keyboard::isKeyPressed(Keyboard::Escape)) {
      window.close();
    }

    window.clear();
    running();              //this calls the states
    window.display();
}
running()
方法调用所有状态:

void Game::running()
{

    switch (state)
    {
        //other cases here

        case s_play:
            play();
            break;

        default:
            break;
    }
}
并且
play()
方法绘制精灵并移动它:

void Game::play()
{
    Texture myTexture;   //I've also tried to declare these outside the method
    Sprite mySprite;

///////////Graphics

    myTexture.loadFromFile("res/img/player.png");

    mySprite.setTexture(myTexture);

//////////Movement

    static sf::Clock clock;
    float dt = clock.restart().asSeconds();
    Vector2f move;

    if (Keyboard::isKeyPressed(Keyboard::A)) 
        {
            move.x--;
        }

        std::cout << mySprite.getPosition().x << "\n";
    }

    if (Keyboard::isKeyPressed(Keyboard::D))
        {
            move.x++;
        }

        std::cout << mySprite.getPosition().x << "\n";
    }

    mySprite.move(move*300.0f*dt);

    window.draw(mySprite);

}
void游戏::play()
{
Texture myTexture;//我还尝试在方法外部声明这些
雪碧糠秕;
///////////图形
myTexture.loadFromFile(“res/img/player.png”);
设置纹理(myTexture);
//////////运动
静态sf::时钟;
float dt=clock.restart().asSeconds();
向量2f移动;
如果(键盘::isKeyPressed(键盘::A))
{
move.x--;
}

std::cout你应该声明
myTextureand
mySprite
在循环之外的一个地方,他们应该呆多久就呆多久。
目前,您在每次迭代中都会再次创建它们,这对性能不好(尤其是
myTexture.loadFromFile(“res/img/player.png”);
)。重新创建也会重置变换(位置、旋转等)。

我不熟悉SFML,因此我希望在这里查看您的代码时不会偏离基准方法创建一个局部变量move。从外观上看,move包含精灵的x和y坐标。由于在play()方法中定义了move,因此 每次代码通过play()方法运行时,都会在堆栈上创建此变量 相应地移动。移动需要在全局范围内,这样它就不会在每次调用play()时重置。 您将myTexture和mySprite移到函数play()之外时是正确的。您还应该将move移到play()方法之外

大概是这样的:

Texture myTexture;   //I've also tried to declare these outside the method
Sprite mySprite;
Vector2f move;

    void Game::play()
    {

    ///////////Graphics

        myTexture.loadFromFile("res/img/player.png");

        mySprite.setTexture(myTexture);

    //////////Movement

        static sf::Clock clock;
        float dt = clock.restart().asSeconds();

        if (Keyboard::isKeyPressed(Keyboard::A)) 
            {
                move.x--;
            }

            std::cout << mySprite.getPosition().x << "\n";
        }

        if (Keyboard::isKeyPressed(Keyboard::D))
            {
                move.x++;
            }

            std::cout << mySprite.getPosition().x << "\n";
        }

        mySprite.move(move*300.0f*dt);

        window.draw(mySprite);

    }
Texture myTexture;//我还尝试在方法外部声明这些
雪碧糠秕;
向量2f移动;
无效游戏::play()
{
///////////图形
myTexture.loadFromFile(“res/img/player.png”);
设置纹理(myTexture);
//////////运动
静态sf::时钟;
float dt=clock.restart().asSeconds();
如果(键盘::isKeyPressed(键盘::A))
{
move.x--;
}

std::每次迭代创建纹理和精灵都会浪费资源。为什么每次调用都要加载纹理并设置精灵?这只是浪费周期。只做一次。也许纹理、精灵和时钟应该是
游戏
类中的成员变量?返回经过的时间,然后在@Somepr重新启动它ogrammerdude。时钟是静态的,所以这不会有什么区别。你显示的代码是你的实际代码,还是你在问题中写的东西?因为你显示的代码是无效的。你真的应该创建一个来显示给我们。@Someprogrammerdude是的,我作为类的成员,在代码中添加了一条注释。关于时钟工作正常,就像在另一个项目中一样。我已经在注释中阐明了这一点。我在外部,如果你看我在代码中的注释是的,但是在哪里?只是在播放功能之外,但在运行功能中也是不好的。它们不在运行功能中,它们在循环之外