Jump to content
This forum will be closing down. Please move to the respective dedicated project forums.

Buttons set to fixedToCamera disappears after game reset.


Recommended Posts

Exactly as it says on the tin.

The buttons in question are on-screen buttons, for mobile devices. Here's my code them.

this.touchBtnUp = this.game.add.button(160, this.game.world.height - 300, 'ctrlBtn_up', this.touchUpCallback, this, 'btn_up.png');
this.touchBtnDown = this.game.add.button(160, this.game.world.height - 150, 'ctrlBtn_down', this.touchDownCallback, this, 'btn_down.png');
this.touchBtnLeft = this.game.add.button(20, this.game.world.height - 150, 'ctrlBtn_left', this.touchLeftCallback, this, 'btn_left.png');
this.touchBtnRight = this.game.add.button(300, this.game.world.height - 150, 'ctrlBtn_right', this.touchRightCallback, this, 'btn_right.png');

this.touchBtnUp.fixedToCamera = true;
this.touchBtnDown.fixedToCamera = true;
this.touchBtnLeft.fixedToCamera = true;
this.touchBtnRight.fixedToCamera = true;

Nothing out of place, except for one thing: 'ctrlBtn_up' doesn't exist as a spritesheet. Since this is a temporary code, I've used temporary assets, which are not in a spritesheet, but individually created. I'm not sure if that has an effect on anything.

Now, come game over, I show a game over layer (if the player wins) or immediately reset the game (if the player loses). Here's my code for those:

  this.gameOverLayer = new GameOverLayer(this.game);
  this.gameOverLayer.fixedToCamera = true;

  console.log("Restart Level");
  this.bIsGameOver = true;
  var slideOut = Phaser.Plugin.StateTransition.Out.ScaleUp;
  slideOut.duration = 1000;

  var slideIn = Phaser.Plugin.StateTransition.In.SlideBottom;
  slideIn.duration = 1000;
  this.game.state.start('gameplay' , slideOut );

endGame() {
  //Finished Level
  console.log("Finished Level");

And the game over layer has a button that essentially does the same thing as the restartGame function.

However, whether the game is over or restarts, the buttons go missing on the next play through (reloading the browser makes them show up again).

I've tried resetting the x and y, but to no avail.

About the only thing that works is to disable the fixedToCamera but that would mean the buttons getting left behind when the player moves.

Anything else I can try?

Link to comment
Share on other sites

I don't know if I'm reading it right, but if the buttons are in the first scene, they'll get removed when you switch to a second scene. That's normal.

Also try it without the plugin (as in don't load the plugin at all).

Link to comment
Share on other sites

Fixed it. The problem is with this code:

this.touchBtnUp = this.game.add.button(160, this.game.world.height - 300, 'ctrlBtn_up', this.touchUpCallback, this, 'btn_up.png');

For some reason, on game restart, the game.world.height changes. So it's less a case of it disappearing than it is getting set up in the wrong place.

For now, I just hard-coded in my usual height, until I figure out why that changes.

Link to comment
Share on other sites


  • Recently Browsing   0 members

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