Javascript 精灵动画卡在第一帧上?
使用Phaser3,我已经预装了一张精灵表并创建了一些动画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,
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的主要功能
代码中发生的情况如下:在每一帧渲染时,游戏都会检查并决定从头开始运行动画,这就是为什么您只看到第一帧,只是因为当它尝试显示下一帧时,您告诉他再次开始播放动画)