Jump to content

How do I save scenes and swap back and forth between them?


Ding Dong
 Share

Recommended Posts

I am trying to create two scenes that I can toggle between.  I have been using this example here:

 https://labs.phaser.io/view.html?src=src\scenes\changing scene.js

but it seems like in that eexample each time it switches between the scenes it creates them again from scratch.  how would I go about saving the current state of a scene and returning to it?

I've tried using sleep/wake and pause/resume, but in each case it seems like the input handling of the other scenes are all active at the same time....

I feel like I am just blanking on the obvious answer but I've been at it for a long time now

Any help is much appreciated!

 

Link to comment
Share on other sites

start will simply stop current scene and start the new one. A stopped scene cannot be wake any more, that is why your scenes are created again and again. switch can be used for that purpose, and when a scene already been created the wake will be called instead. The downsides is that you can not pass data to the scene while awaking.

Link to comment
Share on other sites

Thanks for the answers.

I'm trying to make a map transition in a 2D tile game.  So basically the ability to travel from one room to the next, then back again, to the previous room being in the same state it was when you left.  

My assumption was that each room should be considered a *different scene* using the engine... does that make sense?

So when I change out of the room, I would like all the loop functions of the room scene to stop, and the sprites to disappear, and a new one to start.  Then when I switch back, everything just resumes from where it was.

Would it make more sense to have the whole main game occur on a single scene, then just keep track of the sprites/tiles of each different room in a separate data structure -- then destroy/re-add them every time a room transition happens?

 

---

below is a stripped down example of what I'm trying to make work

When i use sleep/wake, it will switch to scene B, but doesn't switch back to scene A when I click again

When I use "switch" -- clicking once will switch to scene B, but then clicking again switches to scene A and immediately back to scene B again in the same click

Much appreciated again for the feedback -- this is all pretty new to me
 

var SceneA = new Phaser.Class({

    Extends: Phaser.Scene,

    initialize:

    function SceneA ()
    {
        Phaser.Scene.call(this, { key: 'sceneA' });
    },

    preload: function ()
    {
    },

    create: function ()
    {
      this.input.on('pointerdown', function (event) {
        alert("SWITCHING TO SCENE B");
        game.scene.keys['sceneA'].sys.sleep();
        game.scene.keys['sceneB'].sys.wake();
       // game.scene.switch('sceneA', 'sceneB');   This commented line doesn't work either when used instead of sleep/wake
      }, this); 
    },
  
    update: function (time, delta)
    {
    }

});

var SceneB = new Phaser.Class({

    Extends: Phaser.Scene,

    initialize:

    function SceneB ()
    {
        Phaser.Scene.call(this, { key: 'sceneB' });
    },

    preload: function ()
    {
    },

    create: function ()
    {
      this.input.on('pointerdown', function (event) {
        alert("SWITCHING TO SCENE A");
        game.scene.keys['sceneB'].sys.sleep();
        game.scene.keys['sceneA'].sys.wake();
        // game.scene.switch('sceneB', 'sceneA');   This line doesn't work either when used instead of sleep/wake
      }, this);
    },

    update: function (time, delta)
    {
    }
});


var gameConfig = {

  type: Phaser.WEBGL,
  width: SCREEN_WIDTH,
  height: SCREEN_HEIGHT,
  backgroundColor: BACKGROUND_COLOUR,
  parent: 'phaser-example',
  pixelArt: true,
    physics: {
        default: 'arcade',
        arcade: {
            gravity: { y: 0 },
            debug: false
        }
    },
  scene: [SceneA, SceneB]
};

game = new Phaser.Game(gameConfig);

 

 

Link to comment
Share on other sites

The scene switching example itself is broken now. The problem occurs when using input event to switch to other scenes, which I believe is a bug or something needs improvement.

About the room, I guess you have same logic for all the rooms, then it could definitely  be a single scene which loads data of a room and initialize objects. But the scene can also be stopped and created again for every room (just pass room data to new scene and use the create method), so that the scene will take care of objects/events clean up.

Link to comment
Share on other sites

Ok great, so in that case it would make sense to store the grid location of each sprite somewhere else when you leave the room, then the sprites are destroyed, and then when you go back the scene builds them again with the create method using the data you stored?

 

I think that makes sense to me -- thanks!

Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

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