Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/383.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 精灵动画卡在第一帧上?_Javascript_Phaser Framework_Game Development - Fatal编程技术网

Javascript 精灵动画卡在第一帧上?

Javascript 精灵动画卡在第一帧上?,javascript,phaser-framework,game-development,Javascript,Phaser Framework,Game Development,使用Phaser3,我已经预装了一张精灵表并创建了一些动画 import {Scene} from 'phaser'; class BootScene extends Scene { constructor() { super("scene-boot"); } preload() { this.load.spritesheet('px-hero', 'assets/sprites/px-hero.png', { frameWidth: 16,

使用Phaser3,我已经预装了一张精灵表并创建了一些动画

import {Scene} from 'phaser';

class BootScene extends Scene {
  constructor() {
    super("scene-boot");
  }

  preload() {
    this.load.spritesheet('px-hero', 'assets/sprites/px-hero.png', {
      frameWidth: 16,
      frameHeight: 16
    });
    // ...
  }

  create() {
    // ...
    this.anims.create({
      key: 'px-hero-idle',
      frames: this.anims.generateFrameNumbers('px-hero', {
        start: 0,
        end: 2
      }),
      frameRate: 10,
      repeat: -1
    });

    this.anims.create({
      key: 'px-hero-run',
      frames: this.anims.generateFrameNumbers('px-hero', {
        start: 3,
        end: 6
      }),
      frameRate: 10,
      repeat: -1
    });
    // ...
  }
}

export default BootScene;
import {GameObjects} from 'phaser';
const {Sprite} = GameObjects;

class PxHero extends Sprite {
  constructor(config) {
    super(config.scene, config.x, config.y, "px-hero");

    // Add self to scene's physics
    config.scene.physics.world.enable(this);
    config.scene.add.existing(this);

    this.scene = config.scene;

    this.keys = this.scene.input.keyboard.addKeys('W,S,A,D');

    this.speed = 100;
    this.jumpHeight = 300;
  }

  preUpdate(time, delta) {
    const {W, S, A, D} = this.keys;
    const {speed, jumpHeight, body} = this;
    const touchingGround = body.blocked.down;

    if (A.isDown) {
      this.body.setVelocityX(-speed);
      this.setFlipX(true);
    }
    else if (D.isDown) {
      this.body.setVelocityX(speed);
      this.setFlipX(false);
    }
    else {
      this.body.setVelocityX(0);
    }

    if (W.isDown && touchingGround) {
      this.body.setVelocityY(-jumpHeight);
    }

    // Animations
    if (touchingGround) {
      if (body.velocity.x !== 0) {
        this.anims.play('px-hero-run', true); // here
      }
      else {
        this.anims.play('px-hero-idle', true); // and here
      }
    }
  }
}

export default PxHero;
然后在我的Sprite类中(在引导场景链接到的另一个场景中实例化),我尝试播放动画

import {Scene} from 'phaser';

class BootScene extends Scene {
  constructor() {
    super("scene-boot");
  }

  preload() {
    this.load.spritesheet('px-hero', 'assets/sprites/px-hero.png', {
      frameWidth: 16,
      frameHeight: 16
    });
    // ...
  }

  create() {
    // ...
    this.anims.create({
      key: 'px-hero-idle',
      frames: this.anims.generateFrameNumbers('px-hero', {
        start: 0,
        end: 2
      }),
      frameRate: 10,
      repeat: -1
    });

    this.anims.create({
      key: 'px-hero-run',
      frames: this.anims.generateFrameNumbers('px-hero', {
        start: 3,
        end: 6
      }),
      frameRate: 10,
      repeat: -1
    });
    // ...
  }
}

export default BootScene;
import {GameObjects} from 'phaser';
const {Sprite} = GameObjects;

class PxHero extends Sprite {
  constructor(config) {
    super(config.scene, config.x, config.y, "px-hero");

    // Add self to scene's physics
    config.scene.physics.world.enable(this);
    config.scene.add.existing(this);

    this.scene = config.scene;

    this.keys = this.scene.input.keyboard.addKeys('W,S,A,D');

    this.speed = 100;
    this.jumpHeight = 300;
  }

  preUpdate(time, delta) {
    const {W, S, A, D} = this.keys;
    const {speed, jumpHeight, body} = this;
    const touchingGround = body.blocked.down;

    if (A.isDown) {
      this.body.setVelocityX(-speed);
      this.setFlipX(true);
    }
    else if (D.isDown) {
      this.body.setVelocityX(speed);
      this.setFlipX(false);
    }
    else {
      this.body.setVelocityX(0);
    }

    if (W.isDown && touchingGround) {
      this.body.setVelocityY(-jumpHeight);
    }

    // Animations
    if (touchingGround) {
      if (body.velocity.x !== 0) {
        this.anims.play('px-hero-run', true); // here
      }
      else {
        this.anims.play('px-hero-idle', true); // and here
      }
    }
  }
}

export default PxHero;
但出于某种原因,他们只是播放动画的第一帧,然后就被卡住了


以前有人遇到过这种情况吗?到目前为止,我还没有找到任何解决方案。

每帧动画都基于
preUpdate
函数工作,不调用
super.preUpdate
就在该函数中执行某些操作不是最好的主意。这是第一个,第二个代替
preUpdate
尝试使用
update
函数,不要忘记在其中调用
super.update
。 第三,不要使用
preUpdate
update
函数,尝试做你想做的事情。如果您甚至需要使用
update
功能,您可以通过将现场侦听器设置为
update
Phaser.Scenes.Events.update
)事件来实现。这将使您的代码更加清晰易懂,而且您永远不会错误地损害Phaser的主要功能

代码中发生的情况如下:在每一帧渲染时,游戏都会检查并决定从头开始运行动画,这就是为什么您只看到第一帧,只是因为当它尝试显示下一帧时,您告诉他再次开始播放动画)


每帧动画是基于
preUpdate
函数工作的,不调用
super.preUpdate
就在该函数中执行某些操作不是最好的主意。这是第一个,第二个代替
preUpdate
尝试使用
update
函数,不要忘记在其中调用
super.update
。 第三,不要使用
preUpdate
update
函数,尝试做你想做的事情。如果您甚至需要使用
update
功能,您可以通过将现场侦听器设置为
update
Phaser.Scenes.Events.update
)事件来实现。这将使您的代码更加清晰易懂,而且您永远不会错误地损害Phaser的主要功能

代码中发生的情况如下:在每一帧渲染时,游戏都会检查并决定从头开始运行动画,这就是为什么您只看到第一帧,只是因为当它尝试显示下一帧时,您告诉他再次开始播放动画)