How to create a masked content object with mask inside the class?
Mask must be a class member (changes self coordinates according the parent gameobject).
For example:
Green rectangle is a content area.
Red circle is a mask.
Picture 1 before masking:
class MaskedCont extends Phaser.GameObjects.Container {
constructor(scene: Phaser.Scene, x: number, y: number) {
super(scene, x, y);
const rect = scene.add.rectangle(0, 0, 300, 300, 0x00bb00).setOrigin(0);
const mask = scene.make.graphics({x: 0, y: 0}, false);
mask.fillStyle(0xff0000, 1);
mask.fillCircle(150, 150, 150);
this.add(rect);
this.add(mask);
rect.setMask(mask.createGeometryMask());
}
}
export class GameScene extends Phaser.Scene {
constructor() {
super({ key: "GameScene" });
}
create(): void {
const cont = new MaskedCont(this, 50, 50);
this.add.existing(cont);
}
}
Picture 2 after masking: