Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

117
Vistas
`Phaser.GameObjects.Container` seems to force us to use array. Why does it works this way? Is there some consideration under the hood?

I'm trying to understand how Phaser.GameObjects.Container works. Here is the code.

class BootScene extends Phaser.Scene {
  constructor() {
    super();
  }
  create() {
    this.menus = this.add.container();
    var menuItemBlue = new Phaser.GameObjects.Text(this, 10, 30, 'Blue');
    this.menus.add(menuItemBlue);
    console.log(this.menus.length);

    this.menus = this.add.container();
    var menuItemRed = new Phaser.GameObjects.Text(this, 10, 50, 'Red');
    this.menus.add(menuItemRed);
    console.log(this.menus.length);
  }
}

var config = {
  width: 320,
  height: 260,
  scene: [BootScene]
}

var game = new Phaser.Game(config);
<script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>

create() method adds two items to the container though, the length is always 1.

I also tried this

this.menus.add([menuItemBlue, menuItemRed]);
console.log(this.menus.length);

which outputs as expected, that is 2.

It seems that if I add items one by one, only the last one is kept by the container. If I want to keep them all, I have to put all the items in an array and then add the array to the container. Is my understanding correct?

Why does Phaser.GameObjects.Container work this way? Is there some consideration under the hood?

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

The problem is, that you are creating two containers this.menus = this.add.container();.
Remove the second "creation", and it works.
(I took your example and commented the second creating out, and now it works as expected)

just in case: this.add.container() creates a container, and adds it to the scene.

class BootScene extends Phaser.Scene {
  constructor() {
    super();
  }
  create() {
    // First Container
    this.menus = this.add.container();
    var menuItemBlue = new Phaser.GameObjects.Text(this, 10, 30, 'Blue');
    this.menus.add(menuItemBlue);
    console.log(this.menus.length);

    // Second not needed Container
    // this.menus = this.add.container();
    var menuItemRed = new Phaser.GameObjects.Text(this, 10, 50, 'Red');
    this.menus.add(menuItemRed);
    console.log(this.menus.length);
  }
}

var config = {
  width: 320,
  height: 260,
  scene: [BootScene]
}

var game = new Phaser.Game(config);
<script src="https://cdn.jsdelivr.net/npm/phaser@3.55.2/dist/phaser.js"></script>

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda