Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

126
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!