phaserjs / phaser

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
https://phaser.io
MIT License
37.23k stars 7.1k forks source link

Camera worldView is not updated properly when going back and forth between scenes. #4812

Closed smjnab closed 4 years ago

smjnab commented 5 years ago

Version

Description

The worldView of the main camera is not properly updated if you go back and forth between scenes. this.cameras.main.worldView will on first load have a proper width value, but if you leave scene and come back to scene, the value will be 0 at creation, but updated to the correct value after one update.

Example Test Code

Modify this test https://labs.phaser.io/edit.html?src=src/scenes/changing%20scene.js with the below code to see value on load and after one update. Click to go to next scene, click to come back to first scene.

var SceneA = new Phaser.Class({

    Extends: Phaser.Scene,

    initialize:

    function SceneA ()
    {
        Phaser.Scene.call(this, { key: 'sceneA' });
    },

    preload: function ()
    {
        this.load.image('face', 'assets/pics/bw-face.png');
    },

    create: function ()
    {
        this.add.sprite(400, 300, 'face').setAlpha(0.2);

        this.input.once('pointerdown', function () {

            console.log('From SceneA to SceneB');

            this.scene.start('sceneB');

        }, this);

        this.add.text(128, 128,
            "Camera worldView is on create: "
            +this.cameras.main.worldView.width
        );

        setTimeout(()=>{
            this.add.text(128, 160,
                "Camera worldView is after an update: "
                +this.cameras.main.worldView.width
            );
        },1);
    }

});

var SceneB = new Phaser.Class({

    Extends: Phaser.Scene,

    initialize:

    function SceneB ()
    {
        Phaser.Scene.call(this, { key: 'sceneB' });
    },

    preload: function ()
    {
        this.load.image('arrow', 'assets/sprites/longarrow.png');
    },

    create: function ()
    {
        this.arrow = this.add.sprite(400, 300, 'arrow').setOrigin(0, 0.5);

        this.input.once('pointerdown', function (event) {

            console.log('From SceneB to SceneA');

            this.scene.start('sceneA');

        }, this);

        this.add.text(128, 128,
            "Camera worldView is on create: "
            +this.cameras.main.worldView.width
        );

        setTimeout(()=>{
            this.add.text(128, 160,
                "Camera worldView is after an update: "
                +this.cameras.main.worldView.width
            );
        },1);
    },

    update: function (time, delta)
    {
        this.arrow.rotation += 0.01;
    }

});

var config = {
    type: Phaser.AUTO,
    width: 800,
    height: 600,
    backgroundColor: '#000000',
    parent: 'phaser-example',
    scene: [ SceneA, SceneB ]
};

var game = new Phaser.Game(config);

Additional Information

photonstorm commented 4 years ago

This is behaving as expected. The worldView is recalculated in preRender. If you need the values from it prior to that, which is extremely unlikely, you can call Camera.preRender() yourself to double-update them.