misc-preview-scene.rst 1.7 KB

123456789101112131415161718192021222324252627282930313233
  1. .. include:: ../_header.rst
  2. Preview active scene
  3. ~~~~~~~~~~~~~~~~~~~~
  4. The |SceneEditor|_ runs a Phaser scene inside it, which means, it shows you a pixel-perfect image of the scene you are creating. But sometimes you want to see the scene in action, with the physics, the inputs, the animations. For this purpose there is a trick: the **Preview Scene** (``Ctrl+0``) command.
  5. This command opens your game in the default (or configured) browser but adds the `start` parameter to the URL, and sets the name of the current scene as value. If the current scene is a prefab, then it uses the last valid scene.
  6. For example, if you are editing the **LevelCompleted** scene, and you run the **Preview Scene** command, the editor launches the browser and opens the URL:
  7. ``http://localhost:<port>/start=LevelCompleted``
  8. Now, in the code of your game, you have to read the value of the `start` parameter and start the scene with the same name. This is something you have to code manually, however, the built-in Phaser Editor 2D project templates contain the code for reading the ``start`` parameter. It looks like this:
  9. .. code::
  10. if (process.env.NODE_ENV === "development") {
  11. const start = new URLSearchParams(location.search).get("start");
  12. if (start) {
  13. console.log(`Development: jump to ${start}`);
  14. this.scene.start(start);
  15. return;
  16. }
  17. }
  18. The ``process.env.NODE_ENV === "development"`` expression is replaced by webpack_ at compile time, by ``true`` or ``false`` in regarding you are working on a development or production build.
  19. You can place that code in the **Preloader** scene or any other scene after setting up a consistent game state.