首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Kaboom.js:如何将背景设置为图像

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

Stack Overflow用户
提问于 2022-01-02 19:37:57
回答 1查看 1.1K关注 0票数 4

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-02 23:14:12

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

代码语言:javascript
复制
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();
代码语言:javascript
复制
<script src="https://cdn.jsdelivr.net/npm/kaboom@2000.1.8/dist/kaboom.js"></script>

票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70559159

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档