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.
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.
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With