Rectangle

Introduction

Rectangle shape, built-in game object of phaser.

  • Author: Richard Davey

Usage

Create shape object

var rect = scene.add.rectangle(x, y, width, height, fillColor);
// var rect = scene.add.rectangle(x, y, width, height, fillColor, fillAlpha);

Custom class

  • Define class
    class MyRectangle extends Phaser.GameObjects.Rectangle {
        constructor(scene, x, y, width, height, fillColor) {
            super(scene, x, y, width, height, 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 rect = new MyRectangle(scene, x, y, width, height, fillColor);
    

Color

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

No tint methods

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

Other properties

See game object