我正在制作一个kaboom.js游戏,我想将场景的背景设置为图像。我只能找到改变背景颜色的解决方案。我希望有人能帮忙!
发布于 2022-01-02 23:14:12
在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>
https://stackoverflow.com/questions/70559159
复制相似问题