【问题标题】:game.state is undefined in Phaser 3 (TypeError)在 Phaser 3 (TypeError) 中未定义 game.state
【发布时间】:2020-09-20 17:26:54
【问题描述】:

我开始在 Phaser 中编写游戏并准备好游戏状态。它在控制台中弹出“game.state is undefined”类型错误。不知道该怎么做,因为我已经尽我所能尝试解决这个问题。代码如下。

index.html 文件:

<!DOCTYPE html>
<html>
<head>
	<title>Phaser Game</title>
	<meta charset="utf-8">
	<script src="phaser.min.js"></script>
	<script src="state1.js"></script>
</head>
<body>
	<script src="main.js"></script>
</body>
</html>

Main.js 文件:

const game = new Phaser.Game(600, 400, Phaser.AUTO);
game.State.add('state1', demo.state1);
game.State.start('state1');

state1.js 文件:

var demo = {};
demo.state1 = function(){};
demo.state1.prototype = {
	preload: function(){},
	create: function(){},
	update: function(){}
};

【问题讨论】:

    标签: javascript html typeerror phaser-framework


    【解决方案1】:

    游戏对象中没有state。你应该用scene替换它。
    最好使用标准的方式来创建游戏对象。

    var config = {
        type: Phaser.AUTO,
        parent: 'phaser-example',
        width: 600,
        height: 400,
        scene:  {
          preload: function(){},
          create: function(){},
          update: function(){}
        }
    };
    
    var game = new Phaser.Game(config);
    

    更多细节可以在移相器上找到docs
    你可以找到很多有用的例子here

    【讨论】:

      猜你喜欢
      • 2021-05-22
      • 2020-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多