I’m trying to use a class extending Container properties in order to move a character (who has a lifebar, a weapon, etc).
However, if using this.scene.add.container(0,0, [this.sprite]) is fine, a Container instanciation refuse to work:
export default class Character extends Phaser.GameObjects.Container {
/**
* Character group with different sprite parts
*/
constructor(scene, x, y, frame, name) {
super(scene);
this.x = x;
this.y = y;
this.sprite = this.scene.add.sprite(
x,
y,
'unitsSpriteSheet',
this.frame)
.setOrigin(0, 0.3);
this.sprite.name = 'bodyPart';
this.add(this.sprite);
this.scene.add.existing(this);
}
In scene, I can move the container (console show his x/y updating), but the sprite doesn’t move. However, I can update sprite.x/y directly.
update() {
Game.gameMap.player.x += 1; //move containers coordinates, not sprite ones
Game.gameMap.player.sprite.x += 1; //moving sprite directly is fine
}
Am I missing something with Container instanciation ?
I know I can basically use this.scene.add.container, but why using this step when my class could implement Container’s properties ?
I was wondering, if I have an “action” gameObject, containing graphics (such as a rectangle, a text…). Do I have to use an intermediate container ?
I tried to only add the gameObject, but Phaser is crashing. It seems that sub objects, are not understood if put in that way: