【问题标题】:SFML Animation on Keypress only moves one frameKeypress 上的 SFML 动画仅移动一帧
【发布时间】:2018-04-17 17:56:42
【问题描述】:

我试图让我的动画在按下方向键时循环显示两个图像。目前它会在每次按键时切换图像。我一直在看教程,并了解我需要某种计时器来测量每一帧时间,但到目前为止我试图在我的代码中实现的一切都失败了,所以我只是发布我目前有效的代码。

谁能向我解释一下如何实现这个?

    void Frog::up(sf::Event event)
{
    sf::IntRect frogUpAnimation[iNumFrames];
    frogUpAnimation[0] = sf::IntRect(13, 362, 21, 23);
    frogUpAnimation[1] = sf::IntRect(46, 367, 21, 23);

    if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Up)
    {
        audio.frogjumpsound();
        frogSprite.move(0.0f, -55.0f);
        iScoreCounter = iScoreCounter + 10;
        iCurrentFrame++;
        if (iCurrentFrame >= iNumFrames) iCurrentFrame = 0;
        frogSprite.setTextureRect(frogUpAnimation[iCurrentFrame]);
    }
}



int main()
{
    sf::RenderWindow window(sf::VideoMode(800, 800), "Frogger");
    window.setFramerateLimit(60);

    sf::Clock timer;
    float fFrameTime = 1.0f / 60.0f;
    float fElapsedTime;

    sf::Event event;

    Game game;
    Frog frog;
    Timer countdown;
    Text text;
    Audio gameaudio;

    gameaudio.music();

    while (window.isOpen())
    {
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                window.close();
            }

            if (event.type == sf::Event::KeyPressed)
            {
                game.processKeyPress(event.key.code);

            }

            frog.up(event);
            frog.down(event);
            frog.left(event);
            frog.right(event);

        } // Event loop

        countdown.gametime();

        fElapsedTime = timer.getElapsedTime().asSeconds();
        if (fElapsedTime > fFrameTime)
        {
            timer.restart();
        }

        //Update
        game.checkPads(&frog);
        game.checkWin();
        game.gameOver();
        frog.scorecounter(&countdown);
        frog.update(fElapsedTime);
        game.update(fElapsedTime);
        text.update(fElapsedTime);
        countdown.update(fElapsedTime);
        game.collision(&frog);

        // Drawing
        window.clear();
        window.draw(game);
        window.draw(frog);
        window.draw(countdown);
        window.draw(text);
        window.display();
    } // main loop
}

【问题讨论】:

    标签: animation timer sfml elapsedtime


    【解决方案1】:

    首先,当按下正确的键时,我只会调用“frog.up()”、“frog.left()”等。否则,您每次迭代都可能调用 4 个函数,而这些函数绝对什么都不做。

    if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right) {
        frog.right();
    }
    else if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left) {
        frog.left();
    }
    

    至于您的计时器问题,您的 fElapsedTime 和 fFrameTime 看起来有点古怪。看看https://en.sfml-dev.org/forums/index.php?topic=10913.0,但我真的推荐这个指南:https://github.com/SFML/SFML/wiki/Source:-AnimatedSprite

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-08-20
      • 1970-01-01
      • 2023-03-08
      • 1970-01-01
      • 2020-01-07
      • 2017-10-22
      • 1970-01-01
      相关资源
      最近更新 更多