当我按下按钮时,Phaser 3 攻击动画卡在第一帧

问题描述 投票:0回答:1

您好,我们正在使用 Phaser 3 和 Parcel 为大学项目制作游戏。我们在创建玩家角色的攻击动画时遇到了问题。

所以应该发生的是,当按下 X 键时,攻击动画会播放,就是这样,但相反,它会卡在动画的第一帧,这可能是因为当速度为 0 时,会播放另一个动画同时按下 x 键将速度设置为 0,但我们不知道如何更改它以使其正常工作。

这是角色的代码,它是我们调用到另一个场景的组件:

import Phaser from "phaser";
import Enemies from "../components/Enemies";
import Hitbox from "../components/Hitbox";

export default class Player extends Phaser.GameObjects.Sprite {
  constructor(scene, x, y, texture, velocity) {
    super(scene, x, y, texture);
    this.setTexture("C4");

    scene.add.existing(this);
    scene.physics.add.existing(this);
    this.scene = scene;
    this.body.allowGravity = false;
    this.velocity = velocity;
    this.cursor = scene.input.keyboard.createCursorKeys();

    this.xKey = scene.input.keyboard.addKey(Phaser.Input.Keyboard.KeyCodes.X);

    this.KeySave = null;
    this.facingDirection = null;
    this.damageAmount = 100;

  }

  update() {

    this.body.setVelocity(0);

    if (this.cursor.left.isDown) {
      this.body.setVelocityX(-this.velocity);
      this.anims.play("walkingLeft", true);
      this.KeySave = "left";

    } else if (this.cursor.right.isDown) {
      this.body.setVelocityX(this.velocity);
      this.anims.play("walkingRight", true);
      this.KeySave = "right";

    } else if (this.cursor.up.isDown) {
      this.body.setVelocityY(-this.velocity);
      this.anims.play("walkingUp", true);
      this.KeySave = "up";

    } else if (this.cursor.down.isDown) {
      this.body.setVelocityY(this.velocity);
      this.anims.play("walkingDown", true);
      this.KeySave = "down";
    }

    if (this.KeySave !== null) {
      this.facingDirection = this.KeySave;
    }

    if (this.body.velocity.x === 0 && this.body.velocity.y === 0) {
      switch (this.facingDirection) {
        case "left":
          this.anims.play("leftStop");
          break;
        case "right":
          this.anims.play("rightStop");
          break;
        case "up":
          this.anims.play("upStop");
          break;
        case "down":
          this.anims.play("downStop");
          break;
        default:
          this.anims.play("downStop");
      }
    }

    if (this.xKey.isDown) {
      switch (this.facingDirection) {
        case "left":
          this.anims.play("AttackLeft");
          this.body.setVelocity(0);
          break;
        case "right":
          this.anims.play("AttackRight");
          this.body.setVelocity(0);
          break;
        case "up":
          this.anims.play("AttackUp");
          this.body.setVelocity(0);
          break;
        case "down":
          this.anims.play("AttackDown");
          this.body.setVelocity(0);
          break;
        default:
          this.anims.play("AttackDown");
          this.body.setVelocity(0);
      }
    }
  }
}

有什么办法可以让你按下x键并且动画播放完毕后,即使你仍在按下x键,x键也被认为是向上的?

感谢任何回复的人。

frame phaser-framework phaserjs
1个回答
0
投票

主要问题应该是这一行

 if (this.body.velocity.x === 0 && this.body.velocity.y === 0) { ... }

在这里,您总是将动画设置为其他内容。您必须重新排列更改的

if/else
块,或者只是添加进一步的检查,如下所示:

// so only execute when you are not attacking
if (!this.xKey.isDown && this.body.velocity.x === 0 && this.body.velocity.y === 0) 
© www.soinside.com 2019 - 2024. All rights reserved.