fariazz Posted April 10, 2018 Share Posted April 10, 2018 Hi all, I'm trying to restart a scene but as you can see in this video, rebooting a scene doesn't get rid of the sprites of the scene: I was wondering if I'm making a mistake on how I'm restarting the scene, or if this is a bug and I should open a Github issue. Any help would be much appreciated! Link to comment Share on other sites More sharing options...
bobonthenet Posted April 10, 2018 Share Posted April 10, 2018 I'm having the same problem in my game. I asked in the slack channel but I don't think I was understood. Your video seems pretty clear so hopefully, someone has a solution. fariazz 1 Link to comment Share on other sites More sharing options...
onlycape Posted April 10, 2018 Share Posted April 10, 2018 Hi @fariazz and @bobonthenet, I have tried this code in phaser 3.3.0 and it works: var gameScene = new Phaser.Scene('Game'); gameScene.preload = function(){ this.load.image('image1','myimage.png'); } gameScene.create=function(){ var sprite1=this.add.sprite(Math.random()*400,Math.random()*400,'image1'); this.time.delayedCall(1000,function(){ game.scene.stop('Game'); // Without this line, the sprites are not deleted game.scene.start('Game'); }); } var scenes=[]; scenes.push(gameScene); var config={ type: Phaser.AUTO, width: 400, height: 400, scene: scenes }; var game=new Phaser.Game(config); game.scene.start('Game'); Greetings. Link to comment Share on other sites More sharing options...
bobonthenet Posted April 10, 2018 Share Posted April 10, 2018 This doesn't seem to work completely. The camera has to be reset, that isn't a big deal but it looks like there are some stray objects causing problems with collisions. this.matter.world.on('collisionstart', function(event, bodyA, bodyB) { if(bodyA.gameObject.collision) { bodyA.gameObject.collision(bodyB.gameObject); } if(bodyB.gameObject.collision) { bodyB.gameObject.collision(bodyA.gameObject); } }, this); The first time the scene is loaded the above code works fine. The second time around though I have a bullet colliding with an object that has no gameObject and thus no collision method. I can't seem to determine anything useful about the object being picked up by this method but it doesn't exist the first time. I'm guessing that there is something else that I need to manually reset similar to the camera. To reset the camera I'm just doing this.cameras.main.scrollX = 0;. Is there a way to completely reset everything? I'd like the scene to start completely fresh and anything that I want to be retained I will explicitly pass back into the scene with the start method. Link to comment Share on other sites More sharing options...
onlycape Posted April 10, 2018 Share Posted April 10, 2018 Maybe changing in my previous code, game.scene.stop('Game'); with: scenes[0].sys.shutdown(); This "shutdown this Scene and send a shutdown event to all of its systems" . Link to comment Share on other sites More sharing options...
bobonthenet Posted April 10, 2018 Share Posted April 10, 2018 shutdown() gives the same result as before. Link to comment Share on other sites More sharing options...
bobonthenet Posted April 11, 2018 Share Posted April 11, 2018 Can I do what I am trying to do? My game is going to have multiple levels. The main scene gets the level key passed in via the start method. this.scene.start('MainScene', {level: this.nextLevel}); Then within my scene preload method I load the appropriate level json file. this.load.tilemapTiledJSON('map', `${this.level}.json`); The create method should be building the level based on the json file but it is not. Instead, the previous level is there. I think I'm majorly misunderstanding how scenes are supposed to work. Do I need a new scene for each level? I feel like I shouldn't. I used the same strategy with states in Phaser 2. Can this not be done with Phaser 3 or am I going about this in entirely the wrong way? Link to comment Share on other sites More sharing options...
fariazz Posted April 11, 2018 Author Share Posted April 11, 2018 2 hours ago, bobonthenet said: Can I do what I am trying to do? My game is going to have multiple levels. The main scene gets the level key passed in via the start method. this.scene.start('MainScene', {level: this.nextLevel}); Then within my scene preload method I load the appropriate level json file. this.load.tilemapTiledJSON('map', `${this.level}.json`); The create method should be building the level based on the json file but it is not. Instead, the previous level is there. I think I'm majorly misunderstanding how scenes are supposed to work. Do I need a new scene for each level? I feel like I shouldn't. I used the same strategy with states in Phaser 2. Can this not be done with Phaser 3 or am I going about this in entirely the wrong way? I'm with you. The main thing I want to know is if we are doing it right but there is a bug in how scenes are being rebooted, or if we are doing it wrong and missing something important. Link to comment Share on other sites More sharing options...
fariazz Posted April 11, 2018 Author Share Posted April 11, 2018 There is a new Scene manager restart() method coming in Phaser 3.4: https://github.com/photonstorm/phaser/blob/master/CHANGELOG.md#new-features Will give this a try tomorrow Link to comment Share on other sites More sharing options...
bobonthenet Posted April 11, 2018 Share Posted April 11, 2018 I created this new repo that I think more clearly demonstrates the problem. The scene loads the level correctly the first time but when the scene is restarted it does not load correctly. Instead, the first level is loaded again from where it left off before the restart. https://github.com/bobonthenet/sceneproblemsdemo Link to comment Share on other sites More sharing options...
samme Posted April 11, 2018 Share Posted April 11, 2018 Don't use game.scene (the Scene Manager). Use this.scene (the Scene's Scene Plugin): this.scene.start(); // OR (Phaser v3.4.0): this.scene.restart(); Or if you must use game.scene, you need start and stop: game.scene.stop('KEY'); game.scene.start('KEY'); Link to comment Share on other sites More sharing options...
bobonthenet Posted April 11, 2018 Share Posted April 11, 2018 10 minutes ago, samme said: Don't use game.scene (the Scene Manager). Use this.scene (the Scene's Scene Plugin): this.scene.start(); // OR (Phaser v3.4.0): this.scene.restart(); Or if you must use game.scene, you need start and stop: game.scene.stop('KEY'); game.scene.start('KEY'); I am using this.scene.start('scenename'); In an earlier iteration of trying to get this to work I had preceded that with this.scene.stop('scenename'); and that didn't make a difference. Link to comment Share on other sites More sharing options...
fariazz Posted April 11, 2018 Author Share Posted April 11, 2018 6 hours ago, samme said: Don't use game.scene (the Scene Manager). Use this.scene (the Scene's Scene Plugin): this.scene.start(); // OR (Phaser v3.4.0): this.scene.restart(); Or if you must use game.scene, you need start and stop: game.scene.stop('KEY'); game.scene.start('KEY'); Using this.scene.restart() unfortunately still doesn't solve the issue. Sprites are still not cleaned from the scene. Just tested building from the latest master branch commit: Link to comment Share on other sites More sharing options...
samme Posted April 11, 2018 Share Posted April 11, 2018 It seems to work in v3.3.0. Link to comment Share on other sites More sharing options...
bobonthenet Posted April 12, 2018 Share Posted April 12, 2018 This issue is driving me crazy. OK, I clearly see @samme 's example working. I'm doing the same thing but it's not working for me. Maybe because the codepen example isn't newing up a scene object that gets re-used? I don't have time to test that this morning. What I'm seeing is also not just the sprites not getting cleaned but also many other objects. I'll try and make a video demonstrating my example later today or please take a look at the github example I shared. Link to comment Share on other sites More sharing options...
fariazz Posted April 14, 2018 Author Share Posted April 14, 2018 I can confirm this is fixed on my end when using Phaser 3.4 for the `restart` method, however not when using the `rebootScene` method. The following code works fine on my end (sprites get cleared after scene restarting): // called once after the preload ends gameScene.create = function() { // create sprites at random let sprite1 = this.add.sprite(Math.random() * 400, Math.random() * 400, 'treasure'); let sprite2 = this.add.sprite(Math.random() * 400, Math.random() * 400, 'treasure'); let sprite3 = this.add.sprite(Math.random() * 400, Math.random() * 400, 'treasure'); let sprite4 = this.add.sprite(Math.random() * 400, Math.random() * 400, 'treasure'); this.time.delayedCall(3000, function() { this.scene.restart(); }, [], this); }; If instead of using `this.scene.restart()` I type `this.scene.manager.bootScene(this);`, the sprites don't get cleared after scene rebooting - they just accumulate on the screen. Not sure if that's the intended behavior of bootScene but thought it'd be good to mention it. Link to comment Share on other sites More sharing options...
bobonthenet Posted April 15, 2018 Share Posted April 15, 2018 I couldn't get 3.4 to work. Looks like there was a bug related to matter physics but 3.5 fixes everything for me. Link to comment Share on other sites More sharing options...
dat vu quoc Posted August 15, 2020 Share Posted August 15, 2020 The same to me. I restart a scene but the old assets still there! Link to comment Share on other sites More sharing options...
Recommended Posts