javascript - EaselJS 通过基于当前帧的帧进行动画处理

标签 javascript animation html5-canvas easeljs sprite-sheet

我正在使用 EaselJS SpriteSheets,我想知道如何让我的英雄停止平稳运行,因此,如果 SpriteSheet 正在播放“运行”动画并且位于第 5 帧,我需要通过帧进行动画处理6, 7, 8, 8, 8, 7, 6, 5,然后停在 0 处,让我的英雄站住。 我该怎么做?

这是我的 SpriteSheet:

my SpriteSheet

请注意,帧不是连续的:它们从 0 到 4、4 到 0,然后从 5 到 8。第 9 帧未使用。

这是我的 SpriteSheet 代码:

user.hero.bmp = new createjs.Bitmap("Graphics/Fox.png");
user.hero.bmp.cache(0, 0, user.hero.bmp.image.width, user.hero.bmp.image.height);
var data = new createjs.SpriteSheet({
    images: [user.hero.bmp.cacheCanvas],
    frames: {
        width: 30,
        height: 40
    },
    animations: {
        stand: 0,
        run: {
            frames: [0,1,2,3,4,4,4,3,2,1,0,5,6,7,8,8,8,7,6,5],
            next: true,
            speed: .75
        }
    }
});
user.hero = new createjs.Sprite(data, "stand");
// Removed lots of non-relevant things here
movableObjContainer.addChild(user.hero);
movableObj.push(user.hero);

这是我用来在动画之间切换的函数:

function changeAnimation(obj, frameOrAnimation) {
    if (obj.animation != frameOrAnimation) {
        obj.animation = frameOrAnimation;
        obj.gotoAndPlay(frameOrAnimation);
    }
}

函数使用:changeAnimation(user.hero, "stand");

这是最重要的部分,即在股票代码内部运行的动画逻辑:

if ((user.input.left || user.input.right) && !user.hero.jumping) {
    changeAnimation(user.hero, "run");
} else if (!user.hero.jumping && user.hero.animation != "stopFalling" && user.hero.animation != "almostFalling") {
    changeAnimation(user.hero, "stand");
}

最佳答案

当重新提出我的问题时,我更好地理解了我的问题并解决了它。 谢谢 StackOverflow。

所以,我想我需要获取实际的帧号,然后每个刻度加/减 1,直到到达第 0 帧。不幸的是,这种方法过于复杂,显然会损害可维护性。

我通过检查 currentFrame 属性是否等于零解决了我的问题。如果不是,则动画继续,否则动画停止。

这是生成的代码:

if ((user.input.left || user.input.right) && !user.hero.jumping) {
    changeAnimation(user.hero, "run");
} else if (!user.hero.jumping && user.hero.animation != "stopFalling" && user.hero.animation != "almostFalling") {
    if (!(user.hero.animation == "run" && user.hero.currentFrame != 0)) {
        changeAnimation(user.hero, "stand");
    }
}

关于javascript - EaselJS 通过基于当前帧的帧进行动画处理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24966393/

相关文章:

javascript - 使用 TweenLite/GSAP 选择 DOM 对象的最佳方式?

javascript - 根据 Canvas 上设置的 dpi 设置文本位置和大小

javascript - 如何制作 Html5/Javascript Canvas 填充窗口屏幕?

javascript - Jquery/PHP : passing JSON data with Ajax ends up truncated

javascript - 点击提交时打开JS弹窗

javascript - Angular 指令 - 如何使用 JQuery 将 ngModel 和 ng Bind 添加到自定义指令元素?

javascript - 循环中的 HTML 5 Canvas 文本

javascript - jointjs 的缩放问题

c# - 如何在路径上设置渐变动画以在 WPF/WCF 应用程序中可视化数据流

javascript - 悬停时动画表格背景 - Jquery