Connormcwood Posted March 12, 2017 Share Posted March 12, 2017 When in my maingame state I would like for the player to have the ability to use the ESC key and to return to the startmenu. I have attempted to do this by simply using the if keyboard.esc key is down then start state. However this state has already been previously started and all the sprites which were created oirignally are now no longer there. What remains is a photo which is being loaded within the preload state. I have researched the issue and seen parameters added to it to clearWorld but that did not solve the problem. I read that reset an array or something like that would force create to recreate it however I am not sure the name so I ask for help. if(this.escapeKey.isDown){ this.game.state.start("StartScreen",true,false); } myGame.StartScreen = function(game){ this.game = game; }; myGame.StartScreen.prototype = { create: function() { this.style = { font: "30px Arial", fill: "#ffffff", align: "center"}; this.profileStyle = { font: "14px Arial", fill: "#ffffff", align: "right"}; this.ui = this.game.add.sprite(this.world.centerX, this.world.centerY - 120, 'ui', 'green_button00'); this.ui.anchor.set(0.5); this.uitext = this.game.add.text(this.world.centerX, this.world.centerY - 120, "Play Game", this.style); this.uitext.anchor.set(0.5); this.uitext.addColor('#ffffff', 0); this.ui.inputEnabled = true; this.ui.events.onInputDown.addOnce(this.startGame, this); this.ui2 = this.game.add.sprite(this.world.centerX, this.world.centerY - 60, 'ui', 'green_button02'); this.ui2.anchor.set(0.5); this.ui2text = this.game.add.text(this.world.centerX, this.world.centerY - 60, "Highscores", this.style); this.ui2text.anchor.set(0.5); this.ui2text.addColor('#ffffff', 0); this.ui2.inputEnabled = true; this.ui2.events.onInputDown.addOnce(this.startHighscores, this); this.ui3 = this.game.add.sprite(this.world.centerX, this.world.centerY, 'ui', 'green_button04'); this.ui3.anchor.set(0.5); this.ui3text = this.game.add.text(this.world.centerX, this.world.centerY, "Instructions", this.style); this.ui3text.anchor.set(0.5); this.ui3text.addColor('#ffffff', 0); this.ui3.inputEnabled = true; this.ui3.events.onInputDown.addOnce(this.startInstructions, this); this.ui4 = this.game.add.sprite(this.world.centerX, this.world.centerY + 60, 'ui', 'green_button04'); this.ui4.anchor.set(0.5); this.ui4text = this.game.add.text(this.world.centerX, this.world.centerY + 60, "Settings", this.style); this.ui4text.anchor.set(0.5); this.ui4text.addColor('#ffffff', 0); this.ui4.inputEnabled = true; this.ui4.events.onInputDown.addOnce(this.startSettings, this); this.ui5 = this.game.add.sprite(this.world.centerX, this.world.centerY + 120, 'ui', 'green_button04'); this.ui5.anchor.set(0.5); this.ui5text = this.game.add.text(this.world.centerX, this.world.centerY + 120, "Logout", this.style); this.ui5text.anchor.set(0.5); this.ui5text.addColor('#ffffff', 0); this.ui5.inputEnabled = true; this.ui5.events.onInputDown.addOnce(this.startLogout, this); this.picture = this.game.add.sprite(this.world.centerX / 8, this.world.centerY / 8, 'usersProfilePicture' ); this.picture.anchor.set(0.5); this.userName = this.add.text(this.world.centerX / 2.5, this.world.centerY / 7, myGame.fbData.name, this.profileStyle); this.userName.anchor.set(0.5); }, startGame: function (pointer){ this.state.start('MainGameScreen'); }, startSettings: function (pointer){ this.state.start('Settings'); }, startInstructions: function (pointer){ this.state.start('Instructions'); }, startHighscores: function (pointer){ this.state.start('Highscores'); }, startLogout: function (pointer){ redirect(); } } (Same Two States) Link to comment Share on other sites More sharing options...
samme Posted March 13, 2017 Share Posted March 13, 2017 if (this.escapeKey.isDown) { this.game.state.start("StartScreen"); return; } should work. state.start always runs init/preload/create the same way, whether it's the first start or a repeat. Be careful you're not clearing Phaser's cache (check the console). I would remove `this.game = game`, Phaser does that for you. Link to comment Share on other sites More sharing options...
Recommended Posts