Clone a Rectangle

Demonstrates using the Rectangle Clone method to create independent copies of a source rectangle.

Clone a Rectangle, running
800 × 600
GitHub
class Example extends Phaser.Scene
{
    rectangles = [];
    seedRect;
    graphics;

    create ()
    {
        this.graphics = this.add.graphics({ lineStyle: { color: 0x0000aa }, fillStyle: { color: 0x0000aa } });

        this.seedRect = new Phaser.Geom.Rectangle(400, 300, 30, 30);

        this.input.on('pointermove', pointer =>
        {

            Phaser.Geom.Rectangle.CenterOn(this.seedRect, pointer.x, pointer.y);

        });
    }

    update ()
    {
        const clone = Phaser.Geom.Rectangle.Clone(this.seedRect);

        this.rectangles.push(clone);

        this.graphics.clear();

        for (let i = 0; i < this.rectangles.length; i++)
        {
            const rect = this.rectangles[i];

            Phaser.Geom.Rectangle.Inflate(rect, rect.width * 0.05, rect.height * 0.05);

            if (rect.width > 1600)
            {
                this.rectangles.splice(i--, 1);
            }
            else
            {
                this.graphics.strokeRectShape(rect);
            }
        }
    }
}

const config = {
    scale: { mode: Phaser.Scale.FIT },
    width: 800,
    height: 600,
    type: Phaser.AUTO,
    parent: 'game-container',
    scene: Example
};

const game = new Phaser.Game(config);

More in Rectangle46 examples