sammae

Can't restart game?

Recommended Posts

Hi, 

I have a simple Phaser 3 game with a MenuScene and a GameScene. MenuScene loads, player hits the enter key, then GameScene loads. Once you die in GameScene, I have it go back to the MenuScene, but when you press enter to start again, I get this error: Invalid Animation Key, or Key already in use: right for right, left, and turn. I found a thread that mentions something about preloading assets in the MenuScene which I tried, but it did not alter the issue at all. 

Just for simplicity, I've cut out the small pieces relating to this, any ideas on why it's not loading after you restart the game would be greatly appreciated, thanks!

MenuScene

  create() {
    this.key = this.input.keyboard.addKeys({
        'Enter': Phaser.Input.Keyboard.KeyCodes.ENTER
    });

  }

  update() {
    if (this.key.Enter.isDown) {
       this.startGame();
    }
  }

  startGame() {
    this.scene.start('GameScene');
  }

 

 

GameScene

update() {
  if (this.gameOver){
    this.scene.stop('GameScene');
    this.scene.launch('MenuScene');
  }
}

 

Share this post


Link to post
Share on other sites

You're starting/launching the MenuScene again (which is running the create function), and it's trying to define the same inputs again, which were already defined.  You can use an if statement and check if the input is defined, or just sleep/wake the MenuScene when transitioning to the GameScene.  

Share this post


Link to post
Share on other sites
1 hour ago, sammae said:

I get this error: Invalid Animation Key, or Key already in use: right for right, left, and turn. I found a thread that mentions something about preloading assets in the MenuScene which I tried, but it did not alter the issue at all. 

It's from calling animations.add(…) again with the same keys. A common approach is using a dedicated scene for loading assets and animations that runs only once.

Share this post


Link to post
Share on other sites
On 11/13/2018 at 12:14 PM, samme said:

It's from calling animations.add(…) again with the same keys. A common approach is using a dedicated scene for loading assets and animations that runs only once.

 

That makes sense! So I separated out the preload portion into its own scene called LoadScene.js but I'm a little confused on the order. 

 

So I have my LoadScene.js, where I preload all the things I need for the game. In the create section, I create those keys. Now how do I use those newly created keys and assets inside my other two files, MenuScene (which should get called next) and GameScene (the main scene)? I tried importing LoadScene into MenuScene and GameScene, but it's obviously breaking hard.

 

Quote

class LoadScene extends Phaser.Scene {
  constructor(test) {
      super({
          key: 'LoadScene'
      });
  }
  preload() {
    this.load.image('sky', 'assets/forest.png');
    this.load.image('ground', 'assets/grassMid.png');
    this.load.image('form', 'assets/platform2.png');
    this.load.image('star', 'assets/star.png');
    this.load.image('bomb', 'assets/bomb.png');
    this.load.image('stone1', 'assets/stone1.png');
    this.load.image('stone2', 'assets/stone2.png');
    this.load.image('stone3', 'assets/stone3.png');
    this.load.image('snake', 'assets/snake.png');
    this.load.spritesheet('dude', 'assets/dude.png', { frameWidth: 32, frameHeight: 48 });
    this.load.image('image', 'assets/platform1.png');
  }

  create() {

    this.key = this.input.keyboard.addKeys({
        'Enter': Phaser.Input.Keyboard.KeyCodes.ENTER
    });

    this.cursors = this.input.keyboard.createCursorKeys();
  }
}

export default LoadScene;

 

 

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.