Phaser 3 Animation order/priority

Viewed 106

I'm currently try to add animations to my game and until now it workey okay, but now I got a problem and I think I lost the overview a bit.

My controls:
W - Look up
A - Walk left
D - Walk right
Space - Jump
E hold - charge shot (for animations: hold gun while staying/runing/jumping)
E release - shot

My Problem: All animations are fine so far, if I use any control key and press E afterwards. The problem is, if I hold E first and then press any ther key, the current animation doesn't change. Same if I press any key like walking left, press and hold E and release walking while holding E. Only exception is, if I hold E first and press W. Then the look up animation will play. But if I release W while holding E, it's stuck in look up animation again.

My Animation/player control code:

playerControlUpdate() {
    if (this.setUIOnce) {
        this.setUpUI();
        this.setUIOnce = false;
    }

    //Fullscreen
    if (Phaser.Input.Keyboard.JustDown(this.key_F)) {

        if (this.scene.scale.isFullscreen) {
            this.scene.scale.stopFullscreen();
        }
        else {
            this.scene.scale.startFullscreen();
        }
    };

    //movement
    if (this.key_A.isDown) {
        this.setVelocityX(this.pace * -1);
        this.setFlipX(true);
        this.playerDirection = "left";
        if (this.body.touching.down) {
            this.currentFrame = this.anims.currentFrame.index;
            if (this.key_E.isDown) {
                if(this.anims.currentAnim.key == 'walkRight') {
                    if (this.currentFrame >= 13 || this.currentFrame <= 0) {
                        this.play({ key: 'shootWalk', startFrame: 1 }, true);
                    }
                    else {

                        this.play({ key: 'shootWalk', startFrame: this.currentFrame }, true);
                    }
                }

            }
            else {
                if (this.anims.currentAnim.key == 'shootWalk') {
                    if (this.currentFrame >= 13 || this.currentFrame <= 0) {
                        console.log(this.currentFrame);
                        this.play({ key: 'walkRight', startFrame: 1 }, true);
                    }
                    else {

                        this.play({ key: 'walkRight', startFrame: this.currentFrame }, true);
                    }


                }
                else {
                    this.anims.play('walkRight', true);
                }
            }
        }

        
    }
    else if (this.key_D.isDown) {
        this.setVelocityX(this.pace);
        this.setFlipX(false);
        this.playerDirection = "right";
        if (this.body.touching.down) {
            this.currentFrame = this.anims.currentFrame.index;
            if (this.key_E.isDown) {
                if (this.anims.currentAnim.key == 'walkRight') {
                    if (this.currentFrame >= 13 || this.currentFrame <= 0) {
                        console.log(this.currentFrame);
                        this.play({ key: 'shootWalk', startFrame: 1 }, true);
                    }
                    else {

                        this.play({ key: 'shootWalk', startFrame: this.currentFrame }, true);
                    }
                }
                
            }
            else {
                if (this.anims.currentAnim.key == 'shootWalk') {
                    if (this.currentFrame >= 13 || this.currentFrame <= 0) {
                        console.log(this.currentFrame);
                        this.play({ key: 'walkRight', startFrame: 1 }, true);
                    }
                    else {

                        this.play({ key: 'walkRight', startFrame: this.currentFrame }, true);
                    }
                }
                else {
                    this.anims.play('walkRight', true);
                }
            }
            
        }
    }
    else {
        this.setVelocityX(0);
        if (this.body.touching.down && !this.key_E.isDown && this.anims.currentAnim.key != 'shootStay_release' && !this.anims.isPaused && this.anims.currentAnim.key != 'shootUp') {
            if (this.playerDirection == "left") {
                this.setFlipX(true);
                this.anims.play('idle', true);
            }
            else if (this.playerDirection == "right") {
                this.setFlipX(false);
                this.anims.play('idle', true);
            }
        }
        
    }

    if (this.key_W.isDown && !(this.key_A.isDown || this.key_D.isDown)) {
        if (this.anims.currentAnim.key != 'shootUp' && this.body.touching.down) {
            this.anims.play('lookUp', true);
        }
    }
    if (!this.body.touching.down) {
        this.currentFrame = this.anims.currentFrame.index;
        if (this.key_W.isDown) {
            if (this.anims.currentAnim.key == 'jump' || this.anims.currentAnim.key == 'shootJump') {
                if (this.currentFrame >= 8 || this.currentFrame <= 0) {
                    console.log(this.currentFrame);
                    this.play({ key: 'upJump', startFrame: 1 }, true);
                }
                else {

                    this.play({ key: 'upJump', startFrame: this.currentFrame }, true);
                }
            }
        }
        else if (this.key_E.isDown) {
            if (this.anims.currentAnim.key == 'jump' || this.anims.currentAnim.key == 'upJump') {
                if (this.currentFrame >= 8 || this.currentFrame <= 0) {
                    console.log(this.currentFrame);
                    this.play({ key: 'shootJump', startFrame: 1 }, true);
                }
                else {

                    this.play({ key: 'shootJump', startFrame: this.currentFrame }, true);
                }
            }
        }
        else {
            if (this.anims.currentAnim.key == 'upJump' || this.anims.currentAnim.key == 'shootJump') {
                if (this.currentFrame >= 8 || this.currentFrame <= 0) {
                    console.log(this.currentFrame);
                    this.play({ key: 'jump', startFrame: 1 }, true);
                }
                else {

                    this.play({ key: 'jump', startFrame: this.currentFrame }, true);
                }
            }
        }
        
    }


    if (Phaser.Input.Keyboard.JustDown(this.key_SPACE)) {
        if (this.body.touching.down) {
            this.setVelocityY(this.jumpPower);
            if (this.key_W.isDown) {
                this.anims.play('upJump', true);
            }
            else if (this.key_E.isDown) {
                this.anims.play('shootJump', true);
            }
            else {
                this.anims.play('jump', true);
            }
            
        }
    }

    //Weapons
    if (Phaser.Input.Keyboard.JustDown(this.key_E)) {
        this.bigShot = this.scene.time.now + 700;
        if (this.key_W.isDown ) {
            this.playAfterRepeat('lookUp');
        }
        else {
            if (this.anims.currentAnim.key != 'shootStay_release' && !this.anims.isPaused && !(this.key_A.isDown || this.key_D.isDown) && this.body.touching.down) { // touching ground bei jumpshoot ggf anpassen
                this.anims.play('shootStay_hold', true);
            }
            else {
                if (this.body.velocity.x == 0) {
                    this.playAfterRepeat('shootStay_hold');
                }
                
            }
        }

    }

    if (Phaser.Input.Keyboard.JustUp(this.key_E)) {
        if (this.body.touching.down) {
            if (this.key_W.isDown) {
                this.anims.play('shootUp', true);
                this.playAfterRepeat('lookUp');
            }
            else if (this.key_A.isDown || this.key_D.isDown) {}
            else {
                this.anims.play('shootStay_release', true);
                this.playAfterRepeat('idle');
            }
        }
        
        if (this.weaponInUse == 1) {
            this.useWeaponOne();
        }
        else {
            this.useWeaponTwo();
        }

    }

    if (Phaser.Input.Keyboard.JustDown(this.key_C)) {
        if (this.weaponCD > this.scene.time.now) { return; } // big shot
        else {
            this.weaponInUse = this.weaponInUse * -1;
            this.scene.registry.set("playerWeaponInUse", this.weaponInUse);     //später entfernen. Muss nur bei Szenenwechsel gespeichert werden (wenn überhaupt).
            eventEmitter.emit('activeWeapon', this.weaponInUse);
            eventEmitter.emit('weaponCD', this.weaponCooldown);
            this.scene.time.addEvent(this.timedEvent);
            this.weaponCD = this.scene.time.now + this.weaponCooldown;
        }
    }



    if (Phaser.Input.Keyboard.JustDown(this.key_X)) {
    }

    
}

I hope some new perspectives/views can help me. :) If anyone knows a better way to sort animations, I would appreciate it too. I know my order is very... chaotic.

1 Answers

I think the problem is the second parameter in the play function. This parameter tells phaser not to play the animation, when it is playing already (link to the documentation, or checkout this answer).

You just could remove the parameter or set it to false (but only on the shoot animations, and/or where I would make sense).

Related