Jump to content

Scene rebooting issue - Sprites don't disappear


fariazz
 Share

Recommended Posts

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

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

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

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

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

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

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

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

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

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

  • 2 years later...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...