Jump to content

Text not showing on state change


Recommended Posts

I have a game working using states.  It goes from the preload state to the menu, to the game.


When the game loads it displays the text fine, where I can click it to start the game.  However, when the player dies I start the preload sequence again.  The state loads but the text never appears.  It appears to be still there as I can click on the screen to start the game again but I just have a black screen.  I tried adding a tryAgain state to go to this instead but the issue persists.



MyGame.GameState.prototype = {    create: function() {       ....    },    update: function() {       if (this.health == 0) {          this.state.start(tryAgainState)       }    }


MyGame.TryAgainState = function (game) {    this.label;};  MyGame.tryAgainState.prototype = {    create: function() {       this.label = this.add.text(this.world.centerX, this.world.centerY, "Start Over",                                   {  font: "65px Arial",                                     fill: "white",                                     align: "center"                                  });                this.label.anchor.set(0.5);        this.label.inputEnabled = true;              this.input.onDown.add(this.startOver, this);    },    startOver: function() {        this.state.start("mainGameState");    }}
Link to comment
Share on other sites

You have a few typos in your code block above, so I am not sure if it's because of that, which is causing the blank screen.

  • MyGame.tryAgainState.prototype should be MyGame.TryAgainState.prototype
  • this.state.start(tryAgainState) should be this.state.start("tryAgainState")

This is assuming that you set up the states as follows:

game.state.add("tryAgainState", MyGame.TryAgainState);game.state.add("mainGameState", MyGame.GameState);

Basically, be careful of your capitalization and your use of quotes.


However, I recreated your scenario and had no problems with going back and forth between states and seeing the text.

Here is a fiddle: http://jsfiddle.net/chongdashu/c385xpcd/

Link to comment
Share on other sites

Hey chongdashu, 


Fixed the typos, the one missing the quotes was a mistake typing in the question but the problem persisted.


I logged the text and found out that the coordinates weren't being set for some reason.  I tested by just setting them to (200, 200) and the text appears.


Not really sure why this.world.centerX and Y weren't working.





Figured it out.  In my game, I resize the world to be the width of my wide scrolling map, so when I set centerX, it's being placed way out of bounds of the original frame.

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

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.


  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...