sammae Posted November 13, 2018 Share Posted November 13, 2018 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'); } } Link to comment Share on other sites More sharing options...
prob Posted November 13, 2018 Share Posted November 13, 2018 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. samme 1 Link to comment Share on other sites More sharing options...
samme Posted November 13, 2018 Share Posted November 13, 2018 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. Link to comment Share on other sites More sharing options...
sammae Posted November 20, 2018 Author Share Posted November 20, 2018 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; Link to comment Share on other sites More sharing options...
samme Posted November 20, 2018 Share Posted November 20, 2018 No imports, you just need to reference the keys (names) themselves. See the scenes in https://github.com/samme/phaser-parcel/tree/master/src/app. Link to comment Share on other sites More sharing options...
Recommended Posts