javascript - Kaboom.js: how to set the background to an image - Stack Overflow

I'm making a kaboom.js game and I want to set the background of a scene to an image. I can only fi

I'm making a kaboom.js game and I want to set the background of a scene to an image. I can only find solutions on changing the background to a colour. I hope someone can help!

I'm making a kaboom.js game and I want to set the background of a scene to an image. I can only find solutions on changing the background to a colour. I hope someone can help!

Share Improve this question asked Jan 2, 2022 at 19:37 penguins184penguins184 477 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

In Kaboom.js there is no special support for background images. However you can use a regular game object with a sprite ponent as a background image. Here's a simple example:

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/npm/[email protected]/dist/kaboom.js"></script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744907393a4600338.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信