Sign in to follow this  
Connormcwood

Returning to previous state makes added sprites disappear

Recommended Posts

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)
pic1.png.feb150a3574f9be55c92f8ccc1d4baba.pngpic2.png.b26beeffb4bcc584089f0f517cb6a2cb.png 

Share this post


Link to post
Share on other sites
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.

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...
Sign in to follow this  

  • Recently Browsing   0 members

    No registered users viewing this page.