简体   繁体   English

Phaser3添加状态会引发错误。 无法读取未定义的属性“添加”

[英]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.

相关问题 映射对象抛出错误“类型错误:无法读取未定义的属性‘状态’” - mapping object throws error "TypeError: Cannot read property 'state' of undefined" Phaser 3:无法读取未定义的属性“$” - Phaser 3 : Cannot read property '$' of undefined Phaser3 场景切换错误。 我能怎么做? - Phaser3 Scene switching error. How can i do? JavaScript抛出“无法读取未定义的属性……”错误 - JavaScript throws 'cannot read property … of undefined' error 移相器/ TypeError:无法读取未定义的属性“ then” - Phaser / TypeError: Cannot read property 'then' of undefined Phaser:“无法读取未定义的属性” body” - Phaser : 'Cannot read property 'body' of undefined' Facebook登录获取错误。 “无法读取未定义的属性&#39;id&#39;” - Facebook Login fetch error. “Cannot read property 'id' of undefined” Javascript - “无法读取未定义的属性‘closedEls’”和“脚本错误”。 - Javascript - “Cannot read property 'closingEls' of undefined” and “Script error.” OverlayManager错误。 未捕获的TypeError:无法读取未定义的属性&#39;hasOwnProperty&#39; - OverlayManager Error. Uncaught TypeError: Cannot read property 'hasOwnProperty' of undefined 无法读取未定义错误的属性“地图”。 反应 Typescript - Cannot read property 'map' of undefined error. React Typescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM