javascript - 移相器 3 : Scaling the Game to full Websites width and height

标签 javascript html phaser-framework

我是 Phaser 的新手。我希望我的游戏和网站一样大。以下代码将 Phaser Gamefield 缩放到网站的高度和宽度:

width = window.innerWidth * window.devicePixelRatio;
height = window.innerHeight * window.devicePixelRatio;

var config = {
    type: Phaser.AUTO,
    width: width,
    height: height,
    scene: [mainMenu]
};

现在 Canvas 缩放到屏幕,但对象没有。有趣的是:当我将网站的大小调整为 80% 时,一切都非常适合。
但是当它打开 100% 时,背景图像不完整,并且测试不是我放置它的地方。

现在我寻找完美调整 Phaser Gamefield 大小的方法,但一无所获。这时候我对Phaser的了解还太小,不能自己做这个。我希望有人能解释我如何在 Phaser 中扩展我的游戏。

最佳答案

我通常以固定大小构建游戏,然后使用 scale: {}属性以更改其缩放到窗口大小的方式。

在下面的示例中,原始游戏是 1280x720,但缩放模式使其适合窗口。这也应该缩放你的游戏对象。

    let config = {
        width: 1280,
        height: 720,
        // Sets game scaling
        scale: {
            // Fit to window
            mode: Phaser.Scale.FIT,
            // Center vertically and horizontally
            autoCenter: Phaser.Scale.CENTER_BOTH
        },
        scene: []
    };

还有许多其他类型的缩放模式,您可以在 Phaser 的 API 文档中查看:
https://photonstorm.github.io/phaser3-docs/Phaser.Scale.html

希望这有帮助!

关于javascript - 移相器 3 : Scaling the Game to full Websites width and height,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53873534/

相关文章:

javascript - 有没有办法处理在 JavaScript 中调用的未定义函数?

javascript - String.fromCharCode(17) 的结果是什么?

javascript - 通过按钮和 JavaScript 显示或隐藏一个 div

html - 为什么两个边框框 50% 的 div 不是并排的?

javascript - IONIC 第 3 方库

javascript - A-Z 类别 View

javascript - 如果内容太高,底部模态框无法关闭

html - 网站内容位于工具栏旁边而不是下方

javascript - 垂直排序对象,对象之间留有边距空间

javascript - 如何在同一个网页上运行两个或多个不同的 Phaser3 游戏?