rwaikato

Rogue spacebar event firing!

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.

 

 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

  • Recently Browsing   0 members

    No registered users viewing this page.