[英]Phaser3 adding a state throws an error. Cannot read property 'add' of undefined
I am currently trying to make a game with Phaser and I am following an online tutorial. 我目前正在尝试使用Phaser制作游戏,并且正在关注在线教程。 The guy in the video does not get this error even though we have the same code.
即使我们使用相同的代码,视频中的人也不会出现此错误。 Could it have to do with the Phaser version?
可能与Phaser版本有关吗? Or what is it that causes this error?
或是什么原因导致此错误?
This is the error : Uncaught TypeError: Cannot read property 'add' of undefined 这是错误:未捕获的TypeError:无法读取未定义的属性“添加”
Here is the code for the html file where it is thrown. 这是将其引发的html文件的代码。 @ game.state.add('Boot', Game.Boot);
@ game.state.add('Boot',Game.Boot);
<!doctype html> <html> <meta charset="UTF-8" /> <title>My example game</title> <!-- <script src="//cdn.jsdelivr.net/npm/phaser@3.2.1/dist/phaser.js"></script> --> <script src="phaser.js"></script> <script src="Boot.js"></script> <script src="Preloader.js"></script> <script src="MainMenu.js"></script> <script src="Level1.js"></script> <body> <script type="text/javascript"> window.onload = function() { var game = new Phaser.Game(800, 600, Phaser.CANVAS, ''); game.state.add('Boot', Game.Boot); // <---- game.state.add('Preloader', Game.Preloader); game.state.add('MainMenu', Game.MainMenu); game.state.add('Level1', Game.Level1); game.state.start('Boot'); console.log("Done!"); }; </script> </body> </html>
Based upon the comment in your missing head
and question, it looks like you're using Phaser 3. 根据您遗失的
head
和问题中的评论,您似乎正在使用Phaser 3。
In Phaser 3 the State Manager was replaced by Scenes. 在Phaser 3中,状态管理器已替换为场景。 So I would verify that you're referencing Phaser 2 (CE ideally) or 3. Since
phaser.state
is undefined, this suggests version 3 is being used. 因此,我将验证您所引用的是Phaser 2(理想情况下是CE)还是3。由于
phaser.state
是未定义的,因此建议使用版本3。
If you want to continue with Phaser 3 take a look at the official tutorial at https://phaser.io/tutorials/making-your-first-phaser-3-game . 如果要继续使用Phaser 3,请访问https://phaser.io/tutorials/making-your-first-phaser-3-game上的官方教程。
You should also add a <head>
and </head>
to your markup. 您还应该在标记中添加
<head>
和</head>
。 I believe the browser should handle the fact that these are missing, but good practice to follow even for tutorial work. 我认为浏览器应该处理这些缺失的事实,但是即使在进行教程工作时也要遵循良好的做法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.