Phaser 3:允许粒子以翻转/镜像图像发射?

发布于 2025-01-09 10:29:00 字数 146 浏览 0 评论 0原文

我有一个粒子发射器,它像往常一样发射同一图像的多个副本。然而,我希望一些粒子被翻转,要么完全以随机的量翻转,要么在中间翻转,这样落到左边的粒子会被翻转,而落到右边的粒子不会被翻转。

然而,我找不到任何关于翻转粒子而不翻转所有粒子的信息。我只想翻转一些。这有可能吗?

I have a particle emitter which emits multiple duplicates of the same image, as usual. However I'd like some of the particles to be flipped, either completely at a random amount, or sort of in the middle, so that particles falling to the left would be flipped and particles falling to the right won't be.

However I couldn't find anything regarding flipping particles without flipping ALL of them. I'd only like some to be flipped. Is this possible in any way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

把梦留给海 2025-01-16 10:29:00

有几种方法,我认为“最快”就是使用发射器的scaleX属性。

此代码通过与 -1 相乘,翻转大约 10% 的粒子( 0.9 > Math.random() ) ,何时应该翻转。

示例代码:

this.add.particles('sparkle').createEmitter({
    x: 200,
    y: 100,
    scaleX: {
        onEmit: function () { 
            return ( 0.9 > Math.random() ) ? -1 : 1;
        }
    },
    speed: { min: -100, max: 100 },
    quantity: 0.1,
    frequency: 1,
});

但我从之前的问题中假设您的发射器具有“随机比例”属性。在这种情况下,您必须执行以下操作:

示例代码,对于随机缩放的粒子:

gameState.splash = this.add.particles('droplet').createEmitter({
    x: gameState.height/2,
    y: gameState.width/2,
    scale:  { 
        onEmit: function () {
            // create random new scale
            let newRandowmScale = Phaser.Math.FloatBetween(0.05, 0.3);
            return ( 0.9 > Math.random() ) ? -1 * newRandowmScale  : newRandowmScale;
         }
    },
    speed: { min: -100, max: 100 },
    ...
});

更新(SlowerFix):示例代码,对于随机缩放的粒子:

更新的作用:保存 scaleX 事件的当前比例并在 scaleY 中使用它事件。 (它很hacky,但应该可以工作。我会看看是否有更干净的解决方案)

gameState.splash = this.add.particles('droplet').createEmitter({
    x: gameState.height/2,
    y: gameState.width/2,
    scaleY:{
        onEmit: function(particle){
            // keep scale value positive
            return Math.abs(particle.scaleX);
        }
    },
    scaleX:{
        onEmit: function(p){
            let scale = Phaser.Math.FloatBetween(.2, .5);
            return Math.random() > .9 ? scale * -1 : scale;
        }
    }, 
    speed: { min: -100, max: 100 },
    ...
});

There are serveral way's, I think the "fastest" would be just to use the scaleX property of the emiter.

this code flips about 10% of the particles ( 0.9 > Math.random() ), through multiplying it with -1, when it should be flipped.

Example Code:

this.add.particles('sparkle').createEmitter({
    x: 200,
    y: 100,
    scaleX: {
        onEmit: function () { 
            return ( 0.9 > Math.random() ) ? -1 : 1;
        }
    },
    speed: { min: -100, max: 100 },
    quantity: 0.1,
    frequency: 1,
});

But I assume from a earlier question, that you have emitter with a "random scale" property. I that case you woud have to do something like this:

Example Code, for random scaled particles:

gameState.splash = this.add.particles('droplet').createEmitter({
    x: gameState.height/2,
    y: gameState.width/2,
    scale:  { 
        onEmit: function () {
            // create random new scale
            let newRandowmScale = Phaser.Math.FloatBetween(0.05, 0.3);
            return ( 0.9 > Math.random() ) ? -1 * newRandowmScale  : newRandowmScale;
         }
    },
    speed: { min: -100, max: 100 },
    ...
});

UPDATE(SlowerFix): Example Code, for random scaled particles:

What the update does: save the current scale of the scaleX event and use it in the scaleY event. (it is hacky, but should work. I will see if there is a cleaner solution)

gameState.splash = this.add.particles('droplet').createEmitter({
    x: gameState.height/2,
    y: gameState.width/2,
    scaleY:{
        onEmit: function(particle){
            // keep scale value positive
            return Math.abs(particle.scaleX);
        }
    },
    scaleX:{
        onEmit: function(p){
            let scale = Phaser.Math.FloatBetween(.2, .5);
            return Math.random() > .9 ? scale * -1 : scale;
        }
    }, 
    speed: { min: -100, max: 100 },
    ...
});
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文