Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set background color of Phaser 3 Game during runtime?

I am new to Phaser 3 Framework , but have expertise in Angular/Ngrx.

Currently , I am creating a Custom Phaser 3 Game Editor using Angular/Ngrx. Need to modify background color of the Phaser 3 Game at run time.

In Phaser 2+ version , Below code set background color of the game ,

game.stage.backgroundColor = "#4488AA";

But how to set game background color in Phaser 3 irrespective of scene ?

Do we need to set via Camera like below?

this.cameras.main.setBackgroundColor(0xbababa)

Please guide me.

like image 795
Senthil Kumaran C Avatar asked Dec 14 '19 05:12

Senthil Kumaran C


People also ask

How do you change the background color on phaser?

const config = { type: Phaser. AUTO, width: 600, height: 700, backgroundColor: '#4488aa', scene: {...} }; You also have the option of creating a transparent canvas for the game and using a div underneath it to change the background color. To do this, you'll use the transparent property of the GameConfig object.

What is phaser3?

Phaser 3 is the new version of the Phaser Game Framework series. It includes a brand-new custom WebGL renderer designed specifically for the needs of modern 2D games. Phaser uses both a Canvas and WebGL renderer internally and automatically switch between them based on browser support.

How do I change the background color of a phaser scene?

const config = { type: Phaser.AUTO, width: 600, height: 700, backgroundColor: '#4488aa', scene: {...} }; You also have the option of creating a transparent canvas for the game and using a div underneath it to change the background color.

How to initialize a phaser3 canvas?

Let’s explore the initialization of a Phaser3 canvas which is very similar to the previous version, but requires now a configuration object. To initialize the canvas of the game you need to create a new instance of Phaser and pass the desired configuration in one configuration object.

What's new in phaser3?

Phaser is one of the most powerful 2D engines written in JavaScript. Now with Phaser3 it is even more powerful, but some things have changed. Let’s explore the initialization of a Phaser3 canvas which is very similar to the previous version, but requires now a configuration object.

Where can I find the source code for phaser games?

Get the source and assets for every Phaser example from the Phaser Examples GitHub repository. With literally hundreds of pieces of source code this is an invaluable resource to have available locally. The Phaser Sandbox allows you to code games in your browser without having to download or install anything.


1 Answers

If you want the same background color regardless of the active scene, Phaser 3 has a backgroundColor property in the GameConfig object. You can see the documentation here and a working version here.

const config = {
    type: Phaser.AUTO,
    width: 600,
    height: 700,
    backgroundColor: '#4488aa',
    scene: {...}
};

You also have the option of creating a transparent canvas for the game and using a div underneath it to change the background color. To do this, you'll use the transparent property of the GameConfig object. You'll also want to make sure you use the parent property as well with a corresponding <div id="gameContainer"></div> in the HTML to make it easier to grab and modify the color beneath the canvas.

const config = {
    type: Phaser.AUTO,
    width: 600,
    height: 700,
    parent: 'gameContainer',
    transparent: true,
    scene: {...}
};

Then, in the create method, you'll grab that parent item and modify the color:

    create() {
        var div = document.getElementById('gameContainer');
        div.style.backgroundColor = "#4488AA";
    }

You can see a working version here.

like image 199
brae Avatar answered Oct 19 '22 19:10

brae