You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

SFML动画行为异常求助:闲置动画失效、跑动动画延迟

SFML动画脚本异常问题排查求助

我近期在学习SFML,跟随教程开发时发现,教程使用的精灵表中每个动画单独占一行,但我的精灵表布局并非如此,因此重写了动画脚本,却出现异常问题。我使用的精灵表是Adventurer.zip版本的像素英雄资源(非v1.3或v1.5)。

当前问题:闲置动画无法在预期时机停止,且跑动动画出现延迟——添加闲置动画前,跑动动画运行正常。我尝试调整动画起止帧、排查Animation脚本循环逻辑,但问题仍未解决。

Player::Update函数代码

void Player::Update(float deltaTime)
{
    sf::Vector2f movement(0.0f, 0.0f);
    bool running = false;

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::A)) {
        movement.x -= speed * deltaTime;
    }
    if (sf::Keyboard::isKeyPressed(sf::Keyboard::D)) {
        movement.x += speed * deltaTime;
    }
    if (movement.x == 0.0f) {
        running = false;
    }
    else {
        running = true;

        if (movement.x > 0.0f) {
            faceRight = true;
        }
        else {
            faceRight = false;
        }
    }
    if (running == true) {
        animation.Update(sf::Vector2u(1,1), deltaTime, sf::Vector2u(6,1), faceRight);
    }
    else {
        animation.Update(sf::Vector2u(0, 0), deltaTime, sf::Vector2u(4, 0), faceRight);
    }
    body.setTextureRect(animation.uvRect);
    body.move(movement);
}

Animation::Update函数代码

void Animation::Update(sf::Vector2u startingSquare, float deltaTime, sf::Vector2u endingSquare, bool faceRight) {
    totalTime += deltaTime;

    if (totalTime >= switchTime) {
        totalTime -= switchTime;
        currentImage.x++;
        if (currentImage.x >= endingSquare.x && currentImage.y == endingSquare.y) {
            currentImage.x = startingSquare.x;
            currentImage.y = startingSquare.y;
        }
        if (currentImage.y >= imageCount.y) {
            currentImage.x = 0;
            currentImage.y = 0;
        }
        if (currentImage.x >= imageCount.x) {
            currentImage.x = 0;
            currentImage.y++;
        }
    }

    if (faceRight) {
        uvRect.left = currentImage.x * uvRect.width;
        uvRect.width = abs(uvRect.width);
    }
    else {
        uvRect.left = (currentImage.x + 1) * abs(uvRect.width);
        uvRect.width = -abs(uvRect.width);
    }
    uvRect.top = currentImage.y * uvRect.height;
}

恳请帮忙排查问题,谢谢!

内容的提问来源于stack exchange,提问作者The Crazy Creeper

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.15 20:50:29