phaserjs / phaser

Phaser is a fun, free and fast 2D game framework for making HTML5 games for desktop and mobile web browsers, supporting Canvas and WebGL rendering.
https://phaser.io
MIT License
37.11k stars 7.1k forks source link

sprite.anims.nextAnim change to undefined, will not recover from it #5852

Closed Pythux closed 2 years ago

Pythux commented 3 years ago

Version

Description

https://newdocs.phaser.io/docs/3.55.1/Phaser.GameObjects.Sprite#chain https://newdocs.phaser.io/docs/3.55.2/Phaser.Animations.AnimationState#nextAnim

Chaining an animation set anims.nextAnim, if null, to the animation to chain, but after running sprite.play(...) the value turn to undefined, thus the animation to chain goes to anims.nextAnimsQueue leaving anims.nextAnim to undefined forever.

Example Test Code

console.log(sprite.anims.nextAnim) // null
sprite.chain(key)
console.log(sprite.anims.nextAnim) // given key
sprite.play(key)
console.log(sprite.anims.nextAnim) // undefined, it's a bug ?

full running code: (run on https://labs.phaser.io/edit.html)

class Example extends Phaser.Scene
{
    constructor ()
    {
        super();
    }

    preload ()
    {
        this.load.atlas('knight', 'assets/animations/knight.png', 'assets/animations/knight.json');
        this.load.image('bg', 'assets/skies/clouds.png');
    }

    create ()
    {
        //  The background and floor
        this.add.image(400, 16, 'bg').setOrigin(0.5, 0);

        var text = this.add.text(400, 8, 'Click to execute the bug (see logs)', { color: '#ffffff' }).setOrigin(0.5, 0);

        //  Our animations
        this.anims.create({
            key: 'guardStart',
            frames: this.anims.generateFrameNames('knight', { prefix: 'guard_start/frame', start: 0, end: 3, zeroPad: 4 }),
            frameRate: 8
        });

        this.anims.create({
            key: 'idle',
            frames: this.anims.generateFrameNames('knight', { prefix: 'idle/frame', start: 0, end: 5, zeroPad: 4 }),
            frameRate: 8,
            repeat: -1
        });

        var lancelot = this.add.sprite(500, 300)

        lancelot.setScale(8);
        lancelot.play('idle');

        // the bug to run here:
        this.input.on('pointerdown', function () {
            console.log(lancelot.anims.nextAnim) // null
            lancelot.chain('guardStart')
            console.log(lancelot.anims.nextAnim) // guardStart
            lancelot.play('idle')
            console.log(lancelot.anims.nextAnim) // undefined, it's a bug ?

        }, this)

    }
}

const config = {
    type: Phaser.AUTO,
    parent: 'phaser-example',
    width: 800,
    height: 600,
    backgroundColor: '#026bc6',
    pixelArt: true,
    scene: [ Example ]
};

const game = new Phaser.Game(config);
photonstorm commented 2 years ago

Thank you for submitting this issue. We have fixed this and the fix has been pushed to the master branch. It will be part of the next release. If you get time to build and test it for yourself we would appreciate that.