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)
class Game extends Phaser.Scene{ constructor(){ super({ key: "Game", physics: { default: "arcade", arcade: { debug: false } } }); } preload(){ this.load.setBaseURL("assets");
this.load.image("background", "background.png"); this.load.image("play", "play-button.png"); this.load.image("platform", "island.png"); this.load.image("player", "character.png"); for (let i = 1; i <= 4; ++i){ this.load.image(cloud${i}, cloud${i}.png); } } create(){ this.addBackground(); this.addUI(); this.addClouds(); } 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.scene.restart(); } } } - Assistant
generateNextPlatforms(){ // generate new platforms }
generatePlatforms(){ // generate initial platforms } update(){ this.checkPlayerLost(); }}
- 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)
class Game extends Phaser.Scene{ constructor(){ super({ key: “Game”, physics: { default: “arcade”, arcade: { debug: false } } }); } preload(){ this.load.setBaseURL(“assets”);
this.load.image(“background”, “background.png”);
this.load.image(“play”, “play-button.png”);
this.load.image(“platform”, “island.png”);
this.load.image(“player”, “character.png”);
for (let i = 1; i <= 4; ++i){ this.load.image(cloud{i}, cloud{i}.png); } } create(){ this.addBackground(); this.addUI(); this.addClouds(); } 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.scene.restart(); } } }
- Assistant
It seems like the error is occurring because the
this.platformsproperty is not defined in yourGameclass. You need to make sure thatthis.platformsis properly initialized before using it in thecheckPlayerLostmethod.You should define
this.platformsin yourcreatemethod or somewhere else before using it in thecheckPlayerLostmethod. Here's an example of how you can initializethis.platforms:class Game extends Phaser.Scene { constructor(){ super({ key: "Game", physics: { default: "arcade", arcade: { debug: false } } }); } create(){ this.platforms = this.physics.add.group(); // initialize this.platforms this.addBackground(); this.addUI(); this.addClouds(); } 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.scene.restart(); } } } }Make sure to adjust the logic of your game to add platforms to and ensure it is updated correctly before checking if the player is lost.