【问题标题】:How to make canvas responsive using Phaser 3?如何使用 Phaser 3 使画布响应?
【发布时间】:2019-01-02 06:26:20
【问题描述】:

以前我在使用 Phaser 2,但现在我需要切换到 Phaser 3。

我尝试使用 ScaleManager 使画布响应,但它不起作用。

我认为一些方法发生了变化,但我没有找到任何帮助来重新缩放舞台全屏。

var bSize = {
  bWidth: window.innerWidth ||
    root.clientWidth ||
    body.clientWidth,
  bHeight: window.innerHeight ||
    root.clientHeight ||
    body.clientHeight,
};

var game;

var canvas = document.getElementById("canvas");

function create() {

    // Scaling options
    game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;

    // Have the game centered horizontally
    game.scale.pageAlignHorizontally = true;

    // And vertically
    game.scale.pageAlignVertically = true;

    // Screen size will be set automatically
    game.scale.setScreenSize(true);
}

window.onload = function() {

    // Create game canvas and run some blocks
    game = new Phaser.Game(
        bSize.bWidth, //is the width of your canvas i guess
        bSize.bHeight, //is the height of your canvas i guess
        Phaser.AUTO, 
        'frame', { create: create });

    canvas.style.position = "fixed";
    canvas.style.left = 0;
    canvas.style.top = 0;  
}

【问题讨论】:

标签: phaser-framework


【解决方案1】:

Since v3.16.0, use the Scale Manager。简称:

var config = {
    type: Phaser.AUTO,
    scale: {
        mode: Phaser.Scale.FIT,
        parent: 'phaser-example',
        autoCenter: Phaser.Scale.CENTER_BOTH,
        width: 800,
        height: 600
    },
    //... other settings
    scene: GameScene
};

var game = new Phaser.Game(config);

Here 是完整代码,here 是一些使用 Scale Manager 的有用示例。

【讨论】:

  • Uncaught TypeError: Cannot read property 'FIT' of undefined - 我做错了什么?
  • @Jess 似乎应该重命名为 Phaser.Scale.ScaleModes.FIT 或只是硬编码为 3 source code
  • @Vlad 你确定吗?我看到在这个example 中使用了当前版本的 Phase 3.23,它正在使用Phaser.Scale.FIT
  • @Jess 我正在使用 Kotlin,但不知道如何使用 typescript enum 外部定义。所以我只是用幻数硬编码
【解决方案2】:

目前还没有 Phaser 3 的规模管理器,但它正在开发中。现在我建议关注this tutorial。它基本上用一些 CSS 将画布居中,然后调用一个调整大小的函数来处理在窗口发出调整大小事件时保持游戏比例。

这是上面链接的教程中使用的代码: CSS:

canvas {
    display: block;
    margin: 0;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%, -50%);
}

调整大小功能:

function resize() {
    var canvas = document.querySelector("canvas");
    var windowWidth = window.innerWidth;
    var windowHeight = window.innerHeight;
    var windowRatio = windowWidth / windowHeight;
    var gameRatio = game.config.width / game.config.height;

    if(windowRatio < gameRatio){
        canvas.style.width = windowWidth + "px";
        canvas.style.height = (windowWidth / gameRatio) + "px";
    }
    else {
        canvas.style.width = (windowHeight * gameRatio) + "px";
        canvas.style.height = windowHeight + "px";
    }
}

然后:

window.onload = function() {
    //Game config here
    var config = {...};
    var game = new Phaser.Game(config);
    resize();
    window.addEventListener("resize", resize, false);
}

【讨论】:

  • 这对我有用。我必须从游戏中删除 var 并在 onload 之外声明配置以使其正常工作。新代码是window.onload = function () { //Game config here game = new Phaser.Game(config); resize(); window.addEventListener("resize", resize, false); }。现在完美运行:)
【解决方案3】:

尝试将最大宽度添加到画布 css,然后根据纵横比添加最大高度。例如:

canvas {
 display: block;
 margin: 0;
 position: absolute;
 top: 50%;
 left: 50%;
 transform: translate(-50%, -50%);
 max-width: 100%;
 max-height: 50vw;
}

【讨论】:

    【解决方案4】:

    比例管理器有时会处理资产(图像、精灵、..、...)的位置和大小?

    【讨论】:

    • 多描述会更好。
    猜你喜欢
    • 2017-07-24
    • 2022-06-14
    • 2021-05-17
    • 2021-08-11
    • 2014-03-02
    • 1970-01-01
    • 2013-06-23
    • 2015-10-02
    • 2017-02-12
    相关资源
    最近更新 更多