JSFiddle - React, Tailwind, and code Playground

by espace3d

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/phaser/3.16.2/phaser.min.js"></script>
<div id='phaser-example'></div>

JavaScript

var obj;

const my_game = {
    init: () => {
        var main = new Phaser.Class({
            Extends: Phaser.Scene,
            initialize: function main() {
                Phaser.Scene.call(this, {
                    key: 'main'
                });
            },
            create: function () {

                obj = {
                    x: 15
                }
                let _this = this
                window.setInterval(function () {
                    _this.scene.remove("main")
                    _this.scene.start("main")
                }, 2000);

            },
            update: function () {
                if (obj.x > 10) {
                    // just to reveal the problem with state
                    //HERE THE ERROR
                }
            },
        });

        var config = {
            type: Phaser.WEBGL,
            width: 320,
            height: 320,
            backgroundColor: '#fffff',
            parent: 'phaser-example',
            physics: {
                default: 'matter',
                matter: {
                    debug: false,
                    gravity: {
                        y: 0,
                        x: 0,
                    }
                },
            },
            scene: [main]
        }
        var game = new Phaser.Game(config);
    },
}
my_game.init()