Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

127
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda