Phaser3多场景issus。 “未捕获的ReferenceError:未在config.js:17上定义loadScene”

时间:2019-05-27 06:51:27

标签: phaser-framework

我想在Phaser3中制作具有多个场景的游戏。当我尝试使用“ cordova运行浏览器”运行代码时,它呈现灰色屏幕,并且在代码检查控制台中出现错误:“未捕获的ReferenceError:未在config.js:19处定义loadScene”。

///我链接了index.html中的所有文件

   <script type="text/javascript" src="./js/config.js"></script>
   <script type="text/javascript" src="./js/helpers.js"></script>
   <script type="text/javascript" src="./js/loadScene.js"></script>
   <script type="text/javascript" src="./js/mainMenu.js"></script>
   <script type="text/javascript" src="./js/gamePlay.js"></script>
   <script type="text/javascript" src="./js/gameOver.js"></script>```

// set the configuration file config.js 

const gameState = {
   score: 0
};

const config = {
   type: Phaser.AUTO,
   width: 800,
   height: 1368,
   physics: {
       default: 'arcade',
       arcade: { debug: true }
   },
   scene: [loadScene, mainMenu, gamePlay, gameOver] //**here a get the error**
};

const game = new Phaser.Game(config);


// loadScene.js is one of the scenes

class loadScene extends Phaser.Scene {

     constructor() { super({ key: 'loadScene' }); }

     preload() {
         this.load.image('background', '../img/BG/bgVstretch.png');
     }

     create() {
         window.addEventListener('resize', resize);
         resize();

         this.add.image(0, 0, 'background').setOrigin(0);

         this.add.text(100, 100, "Loading Scene", { font: "20px Impact" });

         this.input.on('pointerdown', () => {
             this.scene.stop('loadScene');
             this.scene.start('mainMenu');
         });
     }
 }

// mainMenu.js gamePlay.js gameOver.js....have the same structure as loadScene.js

// helpers.js contains the functions that resizes the game according to the screen.

function resize() {
   var canvas = gameState.game.canvas,
       width = window.innerWidth,
       height = window.innerHeight;
   var wratio = width / height,
       ratio = canvas.width / canvas.height;`

  `if (wratio < ratio) {
       canvas.style.width = width + "px";
       canvas.style.height = (width / ratio) + "px";
  } else {
       canvas.style.width = (height * ratio) + "px";
       canvas.style.height = height + "px";
   }
}```

The game does not render. For now I just wanted it to switch from one scene to another on pointerdown.

1 个答案:

答案 0 :(得分:0)

如果您想为我切换场景,可以使用以下代码

`this.scene.switch('whatever the key is')`

创建矩形后,我编写了此代码,该代码使矩形具有交互性,当您单击它时,它将带您进入下一个层次。

   `advanceButton.setInteractive();          
        advanceButton.on('pointerup', ()=>{
         this.scene.switch('level2')
         })
        });`

关于为什么代码未呈现的原因,请在完成键后删除分号