Jump to content

[solved] Can't change texture of gameobject created with extended Physics.Arcade.Sprite


Recommended Posts

Edit: solved this myself:

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



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")


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

        this.player = this.scene.player

        this.playerAvatarPlaceholder = scene.playerAvatarPlaceholder

        this.playerGroup = scene.playerGroup

    create() {

        const player = this.scene.physics.add
            .sprite(this.x, this.y, this.playerAvatarPlaceholder)

        player.body.onOverlap = true

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

        this.playerGroup.add(player) //!not working in create

    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...

Link to comment
Share on other sites

  • mrmaarten changed the title to [solved] Can't change texture of gameobject created with extended Physics.Arcade.Sprite

  • Recently Browsing   0 members

    • No registered users viewing this page.
  • Create New...