rwaikato Posted June 25, 2014 Share Posted June 25, 2014 Hi Guys, New to this and it may be a simple one... but I have a menu in which you can press the spacebar and the game starts. But once the game has started everytime I press spacebar the game resets by calling the startGame function... I didn't think it would call a function from another state? Below I have the short version of the menu code: var menu = { create: function () { var spaceBarStart = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); spaceBarStart.onDown.add(this.startGame, this); }, startGame: function () { this.game.state.start('play'); },}; Any help will be appreciatedThanks. Link to comment Share on other sites More sharing options...
lewster32 Posted June 25, 2014 Share Posted June 25, 2014 There are several ways to approach this. Probably the best way to do it in this context is as follows:var menu = { create: function () { var spaceBarStart = this.game.input.keyboard.addKey(Phaser.Keyboard.SPACEBAR); // note we're using addOnce here which removes the listener after the first press spaceBarStart.onDown.addOnce(this.startGame, this); }, startGame: function () { this.game.state.start('play'); },};This should work even if you restart the game, as when this state is fired it will create the event again. Link to comment Share on other sites More sharing options...
rwaikato Posted June 25, 2014 Author Share Posted June 25, 2014 Thanks champ that worked perfectly! lewster32 1 Link to comment Share on other sites More sharing options...
Recommended Posts