如何在 Phaser 中 运行 时在角色身后留下一点灰尘痕迹

How to create a little dust trail being left behind the character when running in Phaser

我正在尝试创建一个街机平台,当 运行 时,我的角色会散发出一些小灰尘颗粒。但我仍然可以让它看起来不错,在粒子发射器方面我是超级新手,要更改的变量太多以至于我有点迷路,而且也找不到任何关于 Phaser 粒子发射器的教程,只有一些例子。 我什至从 Phaser 购买了 ParticleStorm,但仍然无法正常工作。

这是Player.tsclass中的粒子代码和字符。

this.game = game;
game.physics.arcade.enable(this);

this.body.gravity.y = Player.GRAVITY;
this.body.velocity.x = 0;
this.body.maxVelocity = 500;

// Allow finger inputs on iOS/Android
this.inputEnabled = true;
this.events.onInputDown.add(this.handleInputDown, this);

// Setup keyboard cursors
this.cursors = this.game.input.keyboard.createCursorKeys();

this.animations.add('running', [5, 6, 7, 8], 20, true);

this.dustEmitter = this.game.add.emitter(0, 0, 5);
this.dustEmitter.makeParticles('dust');
this.dustEmitter.setAlpha(0.9, 1, 200);
this.dustEmitter.setScale(0.3, 1, 0.3, 1, 100, Phaser.Easing.Circular.Out);
// this.dustEmitter.minParticleScale = 0.8;
// this.dustEmitter.maxParticleScale = 1;
// this.dustEmitter.minRotation = -360;
// this.dustEmitter.maxRotation = 360;
this.dustEmitter.setXSpeed(0, 0);
this.dustEmitter.setYSpeed(0, 0);
this.dustEmitter.enableBody = true;
this.dustEmitter.gravity.set(-100, -100);

this.dustEmitter.start(false, 300, 25, -1);

然后在播放器代码的 update() 函数中,我根据播放器的位置更新发射器位置:

if (this.playerState === PlayerState.RUNNING) {
    this.dustEmitter.visible = true;

    this.dustEmitter.emitX = this.x;
    this.dustEmitter.emitY = this.y + (this.height / 2);
} else {
    this.dustEmitter.visible = false;
}

我尝试了很多东西,改变变量,甚至尝试使用 phaser-particle-editor 网站来玩这些值,但我什至无法在我的 Phaser 游戏中复制它,即使我输入了完全相同的值我在网站上。

最好的方法是什么?根据玩家的位置更新发射器位置好吗?还是我应该做点别的?我认为主要想法或我的想法是......粒子实际上不应该跟随玩家,而是留在它们最初出现的地方。一旦玩家开始移动,尘埃就会随着玩家的 X 和 Y 重生。但是我如何使用粒子发射器实现这一点?

提前致谢。

你提到你运行这段代码

if (this.playerState === PlayerState.RUNNING) {
    this.dustEmitter.visible = true;

    this.dustEmitter.emitX = this.x;
    this.dustEmitter.emitY = this.y + (this.height / 2);
} else {
    this.dustEmitter.visible = false;
}

在播放器更新循环中。这意味着即使你正确地设置了一次发射器的位置,只要它是 运行ning(根据 PlayerState),它就会随着玩家一起移动,因为 update() 被调用了一次对于每一帧。您可以做的是引入一个定时事件,该事件会引发一个布尔值,然后您可以使用它来检查是否更新发射器位置。沿着这些线的东西:

class Player extends Phaser.Sprite {
    // ...

    private shouldUpdateEmitterPosition: boolean;

    constructor() {
        // ...

        this.game.time.events.loop(2000, () => this.shouldUpdateEmitterPosition = true, this);
    }

    public update() {
        if (this.shouldUpdateEmitterPosition && this.playerState === PlayerState.RUNNING) {
            this.dustEmitter.visible = true;

            this.dustEmitter.emitX = this.x;
            this.dustEmitter.emitY = this.y + (this.height / 2);

            this.shouldUpdateEmitterPosition = false;   
        } else {
            this.dustEmitter.visible = false;
        }
    }
}