Javascript 第三阶段的新闻动画

Javascript 第三阶段的新闻动画,javascript,animation,2d-games,phaser,Javascript,Animation,2d Games,Phaser,我也是新的JS,一直在尝试用Phaser3创建一个简单的2d游戏。我目前正试图在一个名为“strongAttack”的按键上触发一个攻击动画。由于某种原因,它卡在第一帧上,我不确定我做错了什么。我试过用不同的方法做,但每次都得到相同的结果 Player.js export default class Player extends Phaser.Physics.Matter.Sprite { constructor(data) { let{ scene,x,y,texture,frame

我也是新的JS,一直在尝试用Phaser3创建一个简单的2d游戏。我目前正试图在一个名为“strongAttack”的按键上触发一个攻击动画。由于某种原因,它卡在第一帧上,我不确定我做错了什么。我试过用不同的方法做,但每次都得到相同的结果

Player.js 
export default class Player extends Phaser.Physics.Matter.Sprite {
constructor(data) {
    let{ scene,x,y,texture,frame } = data;
    super(scene.matter.world,x,y,texture,frame);
    this.scene.add.existing(this);

  //this.scene.input.on('pointermove',pointer => this.setFlipX(pointer.worldX < this.x ));

    const { Body,Bodies } = Phaser.Physics.Matter.Matter;
    var playerCollider = Bodies.circle(this.x,this.y,12,{isSensor:false,label:'playerCollider'});
    var playerSensor = Bodies.circle(this.x,this.y,24,{isSensor:true,label:'playerSensor'});
    const compoundBody = Body.create({
        parts:[playerCollider,playerSensor],
        frictionAir: 0.35,
    });
    this.setExistingBody(compoundBody);
    this.setFixedRotation();

}

static preload(scene) {
    scene.load.atlas('knight', 'assets/images/knight.png', 'assets/images/knight_atlas.json');
    scene.load.animation('knight_anim', 'assets/images/knight_anim.json');
    scene.load.spritesheet('items', 'assets/images/items.png',{frameWidth:32,frameHeight:32});
}

get velocity() {
    return this.body.velocity;
}

update() {
    const speed = 2.5;
    let playerVelocity = new Phaser.Math.Vector2();
     if(this.inputKeys.left.isDown) {
        playerVelocity.x = -1; 

         if (this.inputKeys.left.isDown) {
            this.setFlipX(this.velocity.x < -1); 
        } 
    } else if (this.inputKeys.right.isDown) {
        playerVelocity.x = 1;
    }
        if (this.inputKeys.right.isDown) {
            this.setFlipX(this.velocity.x < 1); 
    }
    if(this.inputKeys.up.isDown) {
        playerVelocity.y = -1;
    } else if (this.inputKeys.down.isDown) {
        playerVelocity.y = 1;
    }
    playerVelocity.normalize();
    playerVelocity.scale(speed);
    this.setVelocity(playerVelocity.x,playerVelocity.y);
    if(Math.abs(this.velocity.x) > 0.1 || Math.abs(this.velocity.y) > 0.1) {
        this.anims.play('knight_run',true);
    } else {
        this.anims.play('knight_idle',true); 
    
        **if(this.inputKeys.strongAttack.isDown) {
            this.anims.play('strongAttack')
        console.log('working');**




   
Player.js
导出默认类播放器扩展Phaser.Physics.Matter.Sprite{
建造师(数据){
设{场景,x,y,纹理,帧}=数据;
超级(场景、物质、世界、x、y、纹理、帧);
this.scene.add.existing(this);
//this.scene.input.on('pointermove',pointer=>this.setFlipX(pointer.worldX0.1 | | Math.abs(this.velocity.y)>0.1){
这个。动画。游戏(‘骑士跑’,真的);
}否则{
这个。动画。游戏(‘骑士空闲’,真的);
**if(this.inputKeys.strongAttack.isDown){
这个.anims.play('strongAttack')
console.log('working')**

您应该从create()而不是update()播放动画,因为update会从每帧循环的开始处重新播放动画