# Constructor, scene re-use and parallel instance

**URL:** <https://phaser.discourse.group/t/constructor-scene-re-use-and-parallel-instance/6613>\
**Category:** Phaser 3\
**Created:** [June 10, 2020, 5:45am UTC](https://phaser.discourse.group/t/constructor-scene-re-use-and-parallel-instance/6613 "2020-06-10T05:45:30Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dercetech](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/dercetech/32/6596_2.png) [@dercetech](https://phaser.discourse.group/u/dercetech)\
**Post date:** [June 10, 2020, 5:45am UTC](https://phaser.discourse.group/t/constructor-scene-re-use-and-parallel-instance/6613/1 "2020-06-10T05:45:30Z")

</div>

Greetings!

New to Phaser here, porting my existing game from an ageing tech.

I’m getting trouble understanding the right “Phaser way” to declare a Scene’s instance members; Other discussion make it clear that the constructor only runs once ([example](https://phaser.discourse.group/t/why-does-phaser3-use-init-instead-of-the-constructor/6278)).

My use case is the following:

- I’m using Phaser 3.23 with Typescript (+ Ionic, Capacitor & NgRx so TS is mandatory).
- a “control scene” is started whenever switching levels (even small rooms - so it happens quite often).
- that “control scene” adds the “content scenes” it needs with every switch, like “platformer foreground”, “platformer parallax background”, “mini-map”, “in-game hud”, etc.

First red flag in my approach:

- I have to register a “shutdown” event handler to reset variables like the mock “this.instanceVariable”.
- This, otherwise the variable retains its value from the previous scene run (see init() method).

Hence the concern:

- My platformer game allows to teleport back and forth from the foreground to the background layer.
- The platformer logic is held in a PlatformerScene.  
**- I must run multiple “instances” of that PlatformerScene in parallel and need them to have isolate runtime members.**

So the question, how is this situation usually handled? I don’t imagine I’m doing rocket science, maybe just headed the wrong way 😉

~

Thanks in advance for your time,

NB. The following bit of code shows the “control scene” I’m busy with.

```
export class ControlScene extends Phaser.Scene {
  private layers: Phaser.Scene[] = [];
  private mainLayer: Phaser.Scene;
  private bgParallaxLayer: Phaser.Scene;
  private hudLayer: Phaser.Scene;
  private instanceVariable = 0; // To illustrate the "red flag", see cleanup() below

  constructor() {
    super({ key: "controlScene" });
  }

  init(data) {
    this.events.on("shutdown", this.onShutdown, this);
    console.log("variable: " + this.instanceVariable++);

  }

  create(sceneData: any) {
    const { delegate } = this.plugins.get(DelegatePlugin.ID) as DelegatePlugin;
    /* ... implementation ... */
  }

  run(sceneData: SceneSetData) {
    this.scene.start("controlScene", sceneData);
  }

  /* ... class implementation ... */

  private cleanup() {
    this.layers.forEach((layer) => this.scene.remove(layer));
    this.layers.length = 0;
    this.instanceVariable = 0; // First red flag, this suggest "I'm not using Phaser the right way"
  }

  private onShutdown() {
    this.cleanup();
  }
}
```

---

<div class="post-metadata">

**Author:** ![Darko](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/darko/32/3335_2.png) [@Darko](https://phaser.discourse.group/u/Darko)\
**Post date:** [June 10, 2020, 1:00pm UTC](https://phaser.discourse.group/t/constructor-scene-re-use-and-parallel-instance/6613/2 "2020-06-10T13:00:00Z")

</div>

Hi 👋🏻  
if you are trying to launch same scene each time without sharing its data, and if I understood well, you are looking for something similar to this?

```javascript
for(let i = 0; i < 3; i++) {
    let key = 'Spawn' + i;
    let spawned = new Spawn(key);
    this.scene.add(key, spawned, true);
}

```

Look at it here:

> **[Phaser 3 Dev Log #121: The second part of the huge Scenes tutorial and the...](https://phaser.io/phaser3/devlog/121)**
>
> Desktop and Mobile HTML5 game framework. A fast, free and fun open source framework for Canvas and WebGL powered browser games.

---

<div class="post-metadata">

**Author:** ![dercetech](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/dercetech/32/6596_2.png) [@dercetech](https://phaser.discourse.group/u/dercetech)\
**Post date:** [June 10, 2020, 1:47pm UTC](https://phaser.discourse.group/t/constructor-scene-re-use-and-parallel-instance/6613/3 "2020-06-10T13:47:37Z")

</div>

Hi!  
Thanks indeed it’s what I was looking for. I understand now that the key is used to tell the instance apart.  
Cheers,
