【问题标题】:How to add Lottie Animation in Phaser3?如何在 Phaser3 中添加 Lottie 动画?
【发布时间】:2020-01-01 20:21:41
【问题描述】:

我正在尝试在我的游戏中的 Phaser 3 中添加 Lottie 动画。

但我无法添加。这里的问题是,我将此动画附加到 HTML DOM 元素,因此动画位于 Phaser 游戏画布上方,由于我无法与我的 Phaser 游戏元素交互。我无法在我的 Phaser 游戏画布中包含 Lottie 动画 JSON 文件。

我也尝试让容器成为画布,但它不起作用。

以下是 HTML5 中使用的 Lottie 动画示例:Lottie animation Demo

这是 Lottie 动画文件的链接: Lottie animation JSON file example

这是我为此使用的代码,

 this.animation = bodymovin.loadAnimation({
      container: document.getElementById("game"), // Required
      path: "assets/json/RainLoop.json", // Required
      renderer: "canvas", // Required
      loop: true, // Optional
      autoplay: true, // Optional
      name: "Hello World", // Name for future reference. Optional.
    });

我想将这个 Lottie 动画包含在 Phaser 游戏画布中,但它只包含在我的 HTML DOM 元素中。

【问题讨论】:

    标签: javascript animation html5-canvas phaser-framework lottie


    【解决方案1】:

    可能尚不支持将 Lottie 动画加载到 Phaser 游戏画布中的方式。 Phaser 只能在 preload() 方法中显示通过加载器(例如 this.load.sprite())引入的游戏画布中的对象。

    不幸的是,Lottie json 文件的结构看起来与 expected format -- [example] -- 的 this.load.animation() 加载程序不匹配。

    您可以尝试将 Lottie JSON 文件解析为适当的格式,但这可能会非常复杂。

    如果您想使用 Lottie 动画作为背景,您可以在配置对象中将 Phaser 画布设置为透明,如下所示:

    var config = {
      type: Phaser.AUTO,
      transparent: true,
      ...
    };
    

    然后您可以将 Lottie 动画加载到您的 HTML 中,并使用 CSS 将其放置在您的画布后面。

    【讨论】:

    • 感谢@brae 的回复。我已经尝试将 Lottie 动画放在画布后面并且它正在工作,但是当我为画布移相器游戏放置背景图像时,我无法看到 Lottie 动画,因为 bg 图像会覆盖它。
    • 背景图片是否固定在画布中?还是随着玩家的移动而滚动?
    • 我解决了这个问题。我将我的 bg 保存在一个 div 中,将 bg div 的 z-index 保持为 -10,在此之上我保留 Lottie div。还使我的移相器画布透明。它现在起作用了。
    • 很高兴你明白了。您可以考虑为未来版本的 Phaser 提供 Lottie 动画支持的功能请求。
    • 感谢您的回复。我当然会这样做。
    猜你喜欢
    • 2019-11-30
    • 2020-03-05
    • 2018-12-21
    • 2021-04-04
    • 2018-11-25
    • 2020-08-23
    • 1970-01-01
    • 1970-01-01
    • 2021-12-27
    相关资源
    最近更新 更多