Kaboom.js:如何将背景设置为图像

4

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

1个回答

9

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>


谢谢!我一直在到处寻找答案。 - Alex Wegrzyn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接