【问题标题】:Kaboom.js: how to set the background to an imageKaboom.js:如何将背景设置为图像
【发布时间】:2022-01-02 19:37:57
【问题描述】:

我正在制作一个 kaboom.js 游戏,我想将场景的背景设置为图像。我只能找到将背景更改为颜色的解决方案。希望有人能帮忙!

【问题讨论】:

    标签: javascript 2d-games kaboom


    【解决方案1】:

    在 Kaboom.js 中,没有对背景图像的特殊支持。但是,您可以使用带有精灵组件的常规游戏对象作为背景图像。这是一个简单的例子:

    async function init() {
      kaboom();
      let bgImage = await loadSprite("background", "https://www.paulwheeler.us/files/windows-95-desktop-background.jpg");
    
      let background = add([
        sprite("background"),
        // Make the background centered on the screen
        pos(width() / 2, height() / 2),
        origin("center"),
        // Allow the background to be scaled
        scale(1),
        // Keep the background position fixed even when the camera moves
        fixed()
      ]);
      // Scale the background to cover the screen
      background.scaleTo(Math.max(
        width() / bgImage.tex.width,
        height() / bgImage.tex.height
      ));
    }
    
    init();
    <script src="https://cdn.jsdelivr.net/npm/kaboom@2000.1.8/dist/kaboom.js"></script>

    【讨论】:

    • 谢谢!我一直在到处寻找答案。
    猜你喜欢
    • 2014-06-23
    • 2021-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多