# Can't change Texture on AcradeSprite created with extended class

**URL:** <https://phaser.discourse.group/t/cant-change-texture-on-acradesprite-created-with-extended-class/10754>\
**Category:** Phaser 3\
**Created:** [November 27, 2021, 12:25am UTC](https://phaser.discourse.group/t/cant-change-texture-on-acradesprite-created-with-extended-class/10754 "2021-11-27T00:25:13Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![mrmaarten](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/mrmaarten/32/5905_2.png) [@mrmaarten](https://phaser.discourse.group/u/mrmaarten)\
**Post date:** [November 27, 2021, 12:25am UTC](https://phaser.discourse.group/t/cant-change-texture-on-acradesprite-created-with-extended-class/10754/1 "2021-11-27T00:25:13Z")

</div>

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:

```javascript
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:

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

```

This does not work:

```javascript
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…

---

<div class="post-metadata">

**Author:** ![REALziez](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/realziez/32/5897_2.png) [@REALziez](https://phaser.discourse.group/u/REALziez)\
**Post date:** [November 27, 2021, 12:50am UTC](https://phaser.discourse.group/t/cant-change-texture-on-acradesprite-created-with-extended-class/10754/2 "2021-11-27T00:50:15Z")

</div>

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');`

---

<div class="post-metadata">

**Author:** ![BlunT76](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/blunt76/32/1454_2.png) [@BlunT76](https://phaser.discourse.group/u/BlunT76)\
**Post date:** [November 27, 2021, 7:20am UTC](https://phaser.discourse.group/t/cant-change-texture-on-acradesprite-created-with-extended-class/10754/3 "2021-11-27T07:20:34Z")

</div>

Hi,

Perhaps not the solution, but i do:

```javascript
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:

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

```

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

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

```

---

<div class="post-metadata">

**Author:** ![mrmaarten](https://yyz2.discourse-cdn.com/free1/user_avatar/phaser.discourse.group/mrmaarten/32/5905_2.png) [@mrmaarten](https://phaser.discourse.group/u/mrmaarten)\
**Post date:** [November 29, 2021, 10:12am UTC](https://phaser.discourse.group/t/cant-change-texture-on-acradesprite-created-with-extended-class/10754/4 "2021-11-29T10:12:59Z")

</div>

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…
