【问题标题】:Phaser game not loading移相器游戏未加载
【发布时间】:2019-06-15 18:14:48
【问题描述】:

我正在使用 WampServer 设置一个基本的 Phaser 游戏,我的项目目录位于 WampServer 的 www 目录中,但它给了我一个错误 SceneMainMenu is not defined 为什么以及如何解决这个问题?这是我的文件:

index.html:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Space Shooter</title>
</head>
<body>
  <script src="phaser.js"></script>
  <script src="/scenes/SceneMain.js"></script>
  <script src="/scenes/SceneMainMenu.js"></script>
  <script src="/scenes/SceneGameOver.js"></script>
  <script src="game.js"></script>
</body>
</html>

游戏.js

var config = {
  type: Phaser.WEBGL,
  width: 400,
  height:640,
  backgroundColor: 'black',
  physics: {
    default: 'arcade',
    arcade: {
      gravity: {y: 0}
    }
  },
  scene: [
    SceneMainMenu,
    SceneMain,
    SceneGameOver
  ],
  pixelArt: true,
  roundPixels: true
};

var game = new Phaser.Game(config);

SceneGameOver 文件:

class SceneGameOver extends Phaser.Scene {
  constructor() {
    super({key:'SceneGameOver'})
  }
}

场景主文件:

class SceneMain extends Phaser.Scene {
  constructor() {
    super({key:'sceneMain'});
  }
}

SceneMainMenu 文件:

class SceneMainMenu extends Phaser.Scene {
  constructor() {
    super({key: 'SceneMainMenu'});
  }
  create() {
    this.scene.start('SceneMain');
  }
}

如果我从 VS 代码的服务器运行游戏,它会运行,但是当我尝试从 WampServer 启动它时,它会给我这个错误

【问题讨论】:

  • 你所有的js都加载成功了吗?
  • 只有game.js 其余的我得到服务器响应状态为404(未找到),为什么它们无法加载?
  • SceneMain.js 在哪里?
  • 在场景文件夹中。
  • 由于某种原因 WampServer 没有加载某些文件,但我不知道为什么

标签: javascript wampserver phaser-framework


【解决方案1】:

更新的答案: 重新阅读您的代码后,我发现场景未加载的可能原因可能是场景未处于活动状态。有关如何启动多场景游戏的详细信息,请阅读 Richard Davey 发布的教程:https://phaser.io/phaser3/devlog/121

作为一个简短的答案,在每个场景构造函数中,您可以调用

super({key: 'YourSceneKey', active: true}

在创建游戏时激活场景。 这是我创建的示例 codePen 供您参考 https://codepen.io/arthurcen/pen/yZyaNX

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2015-11-01
  • 2021-06-02
  • 1970-01-01
  • 2016-01-09
  • 1970-01-01
  • 2017-08-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多