I have a camera setup using Bootstrap
import Phaser from 'phaser'
export default class Bootstrap extends Phaser.Scene {
private camera: Phaser.Cameras.Scene2D.Camera | null = null
private zoomLevel: number = 1
constructor() {
super('Bootstrap')
}
private resizeCamera() {
// Responsively set camera size to window size
const width = window.innerWidth;
const height = window.innerHeight;
this.zoomLevel = (width > 600) ? 1.5 : 1;
this.cameras.main.zoom = this.zoomLevel
this.cameras.main.setSize(width, height);
this.cameras.main.setScroll(0, 0);
}
create() {
this.camera = this.cameras.main;
this.resizeCamera();
this.scale.on('resize', this.resizeCamera, this)
// launch GameScene and pass in the camera
this.scene.launch('GameScene', {
existingCamera: this.camera,
});
}
}
I am using this.scene.launch
to run GameScene
alongside Bootstrap
I'm trying to utilize the camera from Bootstrap
in GameScene
import Phaser from 'phaser'
export default class GameScene extends Phaser.Scene {
private player: Phaser.Physics.Arcade.Sprite | null = null;
private camera: Phaser.Cameras.Scene2D.Camera | null = null
constructor() {
super('GameScene')
}
init(data: any) {
this.camera = data.existingCamera;
}
create() {
// Create player sprite and add physics
this.player = this.physics.add.sprite(1200, 1200, 'adam', 18)
this.camera?.setBounds(0, 0, 2400, 2400);
this.camera?.startFollow(this.player, true);
}
}
I use the init function to access the camera from Bootstrap
and apply it in GameScene
. When I console.log
the this.camera
in my create
function, I see that the scene parent is Bootstrap
, but my camera isn't following the player. What am I missing?
Additionally, is it advisable to create a global camera for multiple scenes or should each scene have its own camera, even if they share functionalities?