Phaser 3:允许粒子与 flipped/mirrored 图像一起发射?
Phaser 3: Allow Particles to emit with flipped/mirrored image?
我有一个粒子发射器,它像往常一样发射同一图像的多个副本。然而,我希望一些粒子被翻转,或者完全随机翻转,或者在中间翻转,这样落到左边的粒子会被翻转而落到右边的粒子不会。
但是我找不到任何关于翻转粒子而不翻转所有粒子的信息。我只想要一些被翻转。这有可能吗?
有几种方法,我认为“最快”的方法就是使用发射器的 scaleX
属性。
this code flips about 10% of the particles ( 0.9 > Math.random() )
, through multiplying it with -1
, when it should be flipped.
示例代码:
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 },
...
});
UPDATE(SlowerFix):示例代码,用于随机缩放的粒子:
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 },
...
});
我有一个粒子发射器,它像往常一样发射同一图像的多个副本。然而,我希望一些粒子被翻转,或者完全随机翻转,或者在中间翻转,这样落到左边的粒子会被翻转而落到右边的粒子不会。
但是我找不到任何关于翻转粒子而不翻转所有粒子的信息。我只想要一些被翻转。这有可能吗?
有几种方法,我认为“最快”的方法就是使用发射器的 scaleX
属性。
this code flips about 10% of the particles
( 0.9 > Math.random() )
, through multiplying it with-1
, when it should be flipped.
示例代码:
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 },
...
});
UPDATE(SlowerFix):示例代码,用于随机缩放的粒子:
What the update does: save the current scale of the
scaleX
event and use it in thescaleY
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 },
...
});