Skip to content
Advertisement

how to change character in phaser js i face issue

I set value of character but first time is work fine charter changed but when gameover then if i change the character value will be changed but character still same which i choose first time.

    this.player = this.physics.add.sprite(100, -500, this.charSelect).setOrigin(1, 1);
    this.player.body.setGravityY(1700);
    this.player.setBounce(0.1);

    this.physics.add.existing(this.player).body.setSize(15, 90, ).setOffset(20, 0)

this.charSelect is the variable where charachter name are stored. the value of this.charSelect is change but character still same

    this.load.spritesheet("dude-1", "../src/assets/Character/ch-1.png", {
        frameWidth: 72,
        frameHeight: 92,

    });

    this.load.spritesheet("dude-2", "../src/assets/Character/ch-2.png", {
        frameWidth: 72,
        frameHeight: 92,

    });
    this.load.spritesheet("dude-3", "../src/assets/Character/ch-3.png", {
        frameWidth: 72,
        frameHeight: 92,

    });
    this.load.spritesheet("dude-4", "../src/assets/Character/ch-4.png", {
        frameWidth: 72,
        frameHeight: 92,

    });
    this.load.spritesheet("dude-5", "../src/assets/Character/ch-5.png", {
        frameWidth: 72,
        frameHeight: 92,

    });
    this.load.spritesheet("dude-6", "../src/assets/Character/ch-6.png", {
        frameWidth: 72,
        frameHeight: 92,

    });
    this.load.spritesheet("dude-7", "../src/assets/Character/ch-7.png", {
        frameWidth: 72,
        frameHeight: 92,

    });
    this.load.spritesheet("dude-8", "../src/assets/Character/ch-8.png", {
        frameWidth: 72,
        frameHeight: 92,

    });

here is above character loading in spritesheet

Advertisement

Answer

You would have to call the function setTexture of the sprite object, to change the texture. here is the link to the documentation, after the reselecting of a new character, or restarting of the game:

 this.player.setTexture(this.charSelect);

Update: As aspected the Error has to do with the animations. Since Animations are based/attached on the game object, not the scene (details in the documentation), you would have to remove the animations before recreating them.

Just add this lines, into the GameScene.js before creating the anims, before the line 93:

    this.anims.remove('left');
    this.anims.remove('right');
    this.anims.remove('turn');
    
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement