Polygon

Introduction

Polygon shape, built-in game object of phaser.

  • Author: Richard Davey

Usage

Create shape object

var polygon = scene.add.polygon(x, y, points, fillColor);
// var polygon = scene.add.polygon(x, y, points, fillColor, fillAlpha);
  • points :
    • An array of number : [x0, y0, x1, y1, ...]
    • An array of points : [{x:x0, y:y0}, {x:x1, y:y1}, ...]
    • A string : 'x0 y0 x1 y1 ...'

Note

Shift given points to align position (0, 0)

Custom class

  • Define class
    class MyPolygon extends Phaser.GameObjects.Polygon {
        constructor(scene, x, y, points, fillColor) {
            super(scene, x, y, points, fillColor);
            // ...
            scene.add.existing(this);
        }
        // ...
    
        // preUpdate(time, delta) {}
    }
    
    • scene.add.existing(gameObject) : Adds an existing Game Object to this Scene.
      • If the Game Object renders, it will be added to the Display List.
      • If it has a preUpdate method, it will be added to the Update List.
  • Create instance
    var polygon = new MyPolygon(scene, x, y, points, fillColor);
    

Color

  • Fill color
    • Get
      var color = polygon.fillColor;
      
    • Set
      polygon.setFillStyle(color, alpha);
      
    • Clear
      polygon.setFillStyle();
      
  • Stroke color
    • Get
      var color = polygon.strokeColor;
      
    • Set
      polygon.setStrokeStyle(lineWidth, color, alpha);
      
    • Clear
      polygon.setStrokeStyle();
      

No tint methods

Uses polygon.setFillStyle(color, alpha) to change color.

Smooth

Smooths the polygon over the number of iterations specified.

polygon.smooth(iterations);

Other properties

See game object