Can't change Texture on AcradeSprite created with extended class

I’ve been trying every possibility now for days but nothing works.

What I want to do:
create a player with an Sprite and Physics. And I need to be able to change the Sprite later on during the game.

I made a class creating the Arcade Sprite like so:

export default class playerDefault extends Phaser.Physics.Arcade.Sprite {
    constructor(scene, x, y) {
        super(scene, x, y, "Texture")

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


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

        this.player = this.scene.player

        this.playerAvatarPlaceholder = scene.playerAvatarPlaceholder

        this.playerGroup = scene.playerGroup
        console.log(this.playerGroup)
    }

    create() {

        const player = this.scene.physics.add
            .sprite(this.x, this.y, this.playerAvatarPlaceholder)
            .setDepth(101)
            .setVisible(true)

        player.body.onOverlap = true

        player.setName("playerCreated") //!not working in create

        this.playerGroup.add(player) //!not working in create
        console.log(this.playerGroup.getChildren())
      }

    preUpdate(time, delta) {
        super.preUpdate(time, delta);

        this.rotation += 0.01;
    }
}

In my Scene I need to set the Depth which is already strange, but I can’t change the Texture!

this works:

    this.player.setDepth(102) //needed
    this.player.setName("localPlayer") //working

This does not work:

this.player.setTexture(this.playerAvatarPlaceholder2) //does not work

If I print out the DisplayList I do see the new Texture Key, but it not displaying!

If somebody know how to solve this, would be greatly appreciated…

The way your creating you player seems a bit odd this is how I create mine player = this.physics.add.sprite(2048,2048, 'player');

And if your groups aren’t working this is how I made my groups and children, Group: track = this.add.group(); Children: track1 = track.create(496,3696, 'turn4');

Hi,

Perhaps not the solution, but i do:

export default class Player extends Phaser.GameObjects.Sprite
{
    constructor(scene, x, y, texture)
    {
        super(scene, x, y, texture);
        this.scene = scene;
        this.setDepth(100);
        this.scene.physics.world.enable(this);
        this.scene.add.existing(this);
        this.body.setSize(10, 25, true)
            .setOffset(21, 10)
            .setAllowGravity(true)
            .setCollideWorldBounds(true);
    }
}

And how i create a group in the scene:

this.players = this.physics.add.group({
    classType: Player,
    maxSize: 3,
    allowGravity: true
});

And then in the scene, i can create a player this way:

const playerA = this.players.get(true, x, y, texture, undefined, true);

Thanks for the help!

I was also calling an animation of the previous Texture on the sprite later on in my code, for that reason it reverted back to the previous Texture!

So animations (keys) are connected to Textures (ofcourse). Calling an animation is also a way to change a texture…