Jump to content

Rogue spacebar event firing!


rwaikato
 Share

Recommended Posts

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 appreciated

Thanks.

 

 

Link to comment
Share on other sites

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

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...