This.add.graphics() makes literally zero sense to me

I for the life of me cannot understand this.add.graphics(). Here’s what I “know” (assume):

  • ‘this’ refers to the scene when the statement is placed in the ‘create()’ function.
  • ‘.add’ must be some sort of method that adds the following object to the scene
  • ‘graphics()’ is a game object…?

So why can’t I create an object called graphics then add it to the scene?? I’m guessing I didn’t create it as a “game” object, I just created an object called graphics. How do I create a graphics object separately, then add it to the scene?

‘.add’ is a factory object, each game object plugin(class) will register its creating method into this factory. Usually this creating method has these steps.

  1. New a game object from a class, then
  2. Add this game object into display list, update list of the scene

You can use

var graphics = scene.add.graphics();

to create a new graphics game object, which is equal to

var graphics  = new Phaser.GameObjects.Graphics();
scene.add.existing(graphics);
1 Like

Ahh, that makes more sense. Thanks!!