2015-09-13 64 views
1

当我创建粒子效果时,它们都具有相同的模式。他们旋转,但他们都有相同的模式和相同的彩色粒子。见图片:显示相同粒子的粒子效应 - 不随机化,Monogame

enter image description here

这是一个新的ParticleEffect创建方式:

ParticleEffect p = new ParticleEffect(textures, Vector2.Zero, destination, speed); 

texturesTexture2D名单,VectorZero是起始位置,等等。

每当一个新的ParticleEffect被创建它被添加到列表ParticleList,后来通过所有的项目循环,并呼吁内部的每个效果updatedraw

下面是其中颗粒是随机的:

private Particle GenerateNewParticle() 
{ 
    Random random = new Random(); 
    Texture2D texture = textures[random.Next(textures.Count)]; 
    Vector2 position = EmitterLocation; 
    Vector2 velocity = new Vector2(
      1f * (float)(random.NextDouble() * 2 - 1), 
      1f * (float)(random.NextDouble() * 2 - 1)); 
    float angle = 0; 
    float angularVelocity = 0.1f * (float)(random.NextDouble() * 2 - 1); 
    Color color = new Color(
      (float)random.NextDouble(), 
      (float)random.NextDouble(), 
      (float)random.NextDouble()); 
    float size = (float)random.NextDouble(); 
    int ttl = 20 + random.Next(40); 

    return new Particle(texture, position, velocity, angle, angularVelocity, color, size, ttl); 
} 

randoms有一束,但每个效果仍然出来的相同。 评论,如果你想看到更多的代码。

编辑: 这里是一个粒子被绘制方式:

public void Draw(SpriteBatch spriteBatch) 
{ 
    Rectangle sourceRectangle = new Rectangle(0, 0, Texture.Width, Texture.Height); 
    Vector2 origin = new Vector2(Texture.Width/2, Texture.Height/2); 

    spriteBatch.Draw(Texture, Position, sourceRectangle, Color, 
     Angle, origin, Size, SpriteEffects.None, 0f); 
} 

回答

2

默认情况下,Random实例与当前时间作为种子,这意味着相同的数字序列也会在重新生成启动同时创建实例 - 不要创建新实例,重复使用现有实例以获得更多的“随机”行为(在您的情况下,例如使用静态的Random实例)。