在简单示例中,Phaser 游戏预加载不是 运行

Phaser game preload not running in simple example

我正在学习教程 here,并且...我还没有走多远。

我的代码如下,与示例完全一样。

var game = new Phaser.Game(480, 320, Phaser.AUTO, null, {preload: preload, create: create, update: update});

function preload() {
    game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
    game.scale.pageAlignHorizontally = true;
    game.scale.pageAlignVertically = true;
    game.stage.backgroundColor = '#eee';
}
function create() {}
function update() {}
* { padding: 0; margin: 0; }
<script src="https://end3r.github.io/Gamedev-Phaser-Content-Kit/demos/js/phaser.2.4.2.min.js"></script>

此代码在此处和 jsfiddle 中运行良好,但出于某种原因我无法解释,当我在本地 运行 时它不起作用。我看到 canvas,但预加载代码 运行 的 none。背景为黑色且未对齐。我使用什么浏览器并不重要。为什么会这样?

你需要

this.scale.forcePortrait = true;

应该是这样

function preload() {
    game.scale.scaleMode = Phaser.ScaleManager.SHOW_ALL;
    game.scale.pageAlignHorizontally = true;
    game.scale.pageAlignVertically = true;
    this.scale.forcePortrait = true;
   game.stage.backgroundColor = '#eee';
}

明白了,在版本 3 中,配置选项从 state 更改为 scene。在此处查看文档:https://photonstorm.github.io/phaser3-docs/