如何在移相器框架中切换状态?

时间:2015-04-08 12:59:25

标签: phaser-framework

我试图通过一个功能切换到移相器中的主菜单状态,但无法使其工作。以下是我的代码段。从game.js文件中的update函数调用end函数。

end: function(){
    player.kill();
    ltext.setText("Over!!");
    this.state.start('Menu');
},

1 个答案:

答案 0 :(得分:1)

您最好不要从更新功能中调用此功能。

相反,您可以从精灵或按钮事件处理程序等调用它。

我在更新功能中测试了game.state.start(' xx'),它按预期工作。



window.addEventListener('load', function(){
	var game = new Phaser.Game(500, 190, Phaser.CANVAS, '', {
		create : function (game) {
		    var textStyle = { font: "14px Arial", fill: "#ffcc00" };
		    game.add.text(60, 40, 'Phaser HTML5 Game Engine', textStyle);
		    game.add.text(60, 70, 'This is state 1', textStyle);
          
		    var textStyle = { font: "14px Arial", fill: "#00ff00"};
		    game.add.text(200, 130, 'Pointer here to enter state2', textStyle);
		    var graphics = game.add.graphics(0, 0);
		    graphics.beginFill(0x00ff00);
		    graphics.drawRect(200, 80, 50, 50);
		    graphics.endFill();
	    },
		update : function (game) {
		    var x = game.input.x, y = game.input.y;
		    if(x > 200 && x < 250 && y > 80 && y < 130){
			    game.state.start('state2');
		    }
	    }
	});

	game.state.add('state2', {
		create: function (game){
			var textStyle = { font: "14px Arial", fill: "#00ff00"};
		    game.add.text(60, 40, 'Phaser HTML5 Game Engine', textStyle);
		    game.add.text(60, 70, 'Welcome to state 2', textStyle);
		}
	});
}, false);
&#13;
body{text-align:center;margin:4px;padding:0;}
canvas{vertical-align:middle; background-color:#000;}
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/2.3.0/phaser.min.js"></script>
&#13;
&#13;
&#13;