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

128
Vistas
Phaser 3 this.add.graphics works inside create function, but not outside?

When I add graphics to a scene with this.add.graphics inside of the create function, it works fine, graphics are added. When I try the same code inside of a custom function, say a function called redrawGame(), I get an error message saying:

Undefined is not an object

How can add graphics to a scene outside of the create function?

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

0

Depening on your code, you probably will have to pass the scene ( in this case this) to the custom function, since the this object in the custom function referes to a different object.

 // if you want to call it from the "create" function
 function create(){
     redraw(this);
     ...
 }
 ...
 // Or if you want to call it from the "update" function
 function update(){
     redraw(this);
     ...
 }

 ...

 function redraw(scene){
     scene.add.graphics();
     ...
 }

But without seeing your code, I can't say, if this is your issue, or if there is a different problem.

Update, for custom function call on window resize-event :

To check for the resize Event of the browser, I would use the phaser events (link to documentation:

 // in the create function
 this.scale.on('resize', resizeGame, this);

the third parameter is the scope*(=scene)*, so your resizeGame function would have to call redraw, like this:

function resizeGame(){
   ...
   redraw(this); // due to the scope passed third parameter of 'this.scale.on'
   ...
}
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