In phaser 3 I get this error when restarting scene phaser.min.js:1 Uncaught TypeError:…
- User
In phaser 3 I get this error when restarting scene
phaser.min.js:1 Uncaught TypeError: Cannot read properties of undefined (reading 'sys') at initialize [as constructor] (phaser.min.js:1:152081) at initialize [as constructor] (phaser.min.js:1:360092) at new initialize (phaser.min.js:1:628349) at initialize.create (phaser.min.js:1:238737) at Game.generateNextPlatforms ((index):116:19) at Game.generatePlatforms ((index):177:9) at Game.update ((index):223:8) at initialize.step (phaser.min.js:1:966940) at initialize.update (phaser.min.js:1:954010) at initialize.step (phaser.min.js:1:76119)
<script src="https://cdn.jsdelivr.net/npm/phaser@3.80.1/dist/phaser.min.js"></script> <title> Game </title> - Assistant
The error you are encountering is due to the fact that when you restart the scene using
this.scene.restart(), the scene's objects are still being referenced which causes theCannot read properties of undefined (reading 'sys')error.To fix this issue, you can add a check before referencing any objects in your update method. You can modify the
updatemethod in yourGameclass like this:update(){ if (!this.registry.get("resetting")) { this.checkPlayerLost(); this.updateCameraBounds(); this.generatePlatforms(); this.removeObjects(); } }And then, when calling
this.scene.restart()in thecheckPlayerLostmethod, set a flag in the game's registry to indicate that the scene is being reset:checkPlayerLost(){ if (this.player && !this.player.lost){ const lastPlatform = this.platforms.getChildren()[0]; if (this.player.y > lastPlatform.y + 500){ this.player.lost = true; this.registry.set("resetting", true); // Set resetting flag this.scene.restart(); } } }