Jump to content

Switch background image when call onOrientationChange


eu_genes
 Share

Recommended Posts

Hi!

I have two background images : 'bg_1.png' in resolution 1280x720 for landscape mode and 'bg_2.png'  - 720x1280 for portrait.

and I need switch these images when game orientation change from landscape to portrait and contrariwise.

My code

class Menu extends Phaser.State{
  create(){
    bg1 = this.add.sprite(0, 0, 'bg_1');
    bg1.anchor.setTo(0.5, 0.5);
    bg1.kill();
    bg2 = this.add.sprite(0, 0, 'bg_2');
    bg2.anchor.setTo(0.5, 0.5);
    bg2.kill();
    if (this.scale.isGameLandscape) {
      bg1.revive();
      bg1.reset(this.game.width/2, this.game.height/2);
    };
    if (this.scale.isGamePortrait) {
      bg2.revive();
      bg2.reset(this.game.width/2, this.game.height/2);
    };
    this.scale.onOrientationChange.add(()=>{
       if (this.scale.isLandscape) {
        this.bg2.kill();
        this.scale.setGameSize(1280, 720);
        this.bg1.revive();
        this.bg1.reset(this.game.width/2, this.game.height/2);
      };
      if (this.scale.isPortrait) {
        this.bg1.kill();
        this.scale.setGameSize(720, 1280);
        this.bg2.revive();
        this.bg2.reset(this.game.width/2, this.game.height/2);
      };
      this.scale.refresh();
    }, this);
  }
}

But this works only if change from portrait to landscape.

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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