Creating player with extended class creates dubble sprites

Hi!

I am creating my player with an extended Sprite class

export default class playerDefault extends Phaser.GameObjects.Sprite {
    constructor(scene, x, y) {
        super(scene)
        this.scene = scene;

        // this.scene.add.existing(this)
        // this.scene.physics.add.existing(this)

        this.x = x;
        this.y = y;

        this.player = this.scene.player
        this.playerAvatarPlaceholder = this.scene.playerAvatarPlaceholder
        
        this.player = this.scene.physics.add
            .sprite(this.x, this.y, this.playerAvatarPlaceholder)
          
        this.player.body.onOverlap = true
    }
}

I create my player in the game scene with:

this.player = this.physics.add.existing(new playerDefault(this, 100, 100))

It works, it creates a player that moves, but there is a second sprite created, that appears when the player moves.

Adding .setVisible(false) to the class, hides this second sprite, but I find it strange that it is created in the first place, especially by the class.

Anybody idea’s why this is happening?

Thanks!

I’m pretty sure you are creating 2 sprites. If you send me more of your code I might be able to help you fix it.

class playerDefault extends Phaser.GameObjects.Sprite {
  constructor(scene, x, y, texture) {
    super(scene, x, y, texture);

    this.scene.add.existing(this);
    this.scene.physics.add.existing(this);

    this.body.onOverlap = true;
  }
}
// In scene:
this.player = new playerDefault(this, 100, 100, 'texture');

new playerDefault(…) is the sprite. It shouldn’t call physics.add.sprite(…), which would add a second sprite.

Ah yes, I see. That is the explanation. Thanks!