I have a problem scaling my game, mostly confusion I believe. Basically, I want the game canvas to resize in a medium range and behave like normal Phaser.Scale.FIT behaviour for low and huge resolutions. (Scale down on small screens, scale up on huge screens and do true canvas resizing in between)
For this test I’m using Phaser.Scale.FIT
and want to switch the game from e.g. 800x600 to some arbitrary window size while the game is running.
Only applying this.sys.game.scale.setGameSize(window.innerWidth, window.innerHeight);
does not work as I thought it would. The canvas size is updated as expected but the game is still squeezing into the old canvas size once the auto scaling via FIT
takes over.
Test 1
The Code is as follows (resize controlled via a button press inside the scene):
// scale config
...
scale: {
mode: Phaser.Scale.FIT ,
// autoCenter: Phaser.Scale.CENTER_BOTH,
parent: "phaser-game",
width: 800,
height: 600
}
...
// scene
...
update() {
if (this.keys.spacebar.isDown) {
// From reading the docs this is all I thought have do do in `FIT mode
this.sys.game.scale.setGameSize(window.innerWidth, window.innerHeight);
}
}
...
Result:
Test 2
If I apply the following it is kinda working until the window is resized again:
update() {
if (this.keys.spacebar.isDown) {
this.sys.game.scale.setGameSize(window.innerWidth, window.innerHeight);
this.physics.world.setBounds(0, 0, window.innerWidth, window.innerHeight);
this.sys.game.canvas.style.width = window.innerWidth + 'px';
this.sys.game.canvas.style.height = window.innerHeight + 'px';
}
}
Result:
Test 3 / Hack
After some research I found this issue and tried the described workaround just for the fun and it behaves as I would expected setGameSize()
would work.
I’m using 3.19.0 so it should be already fixed since v3.18.1.
I could not find the commit connected to #4482 to dive deeper into it it
So this is hack (?) is working fine but I’d rather do it the “right way” and not start my new project with a hack right at the beginning
From reading the docs I feel like I shouldn’t be doing this but rather call setGameSize()
in FIT mode and be done wirth it. Am I wrong here or what am I missing?
this.sys.game.scale.setGameSize(window.innerWidth, window.innerHeight);
this.scale.parentSize.setSize(this.scale.canvasBounds.width, this.scale.canvasBounds.height);
this.scale.displaySize.setAspectRatio(this.scale.baseSize.aspectRatio);
this.physics.world.setBounds(0, 0, window.innerWidth, window.innerHeight);
Result:
Thanks!