2015-09-13 62 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實例)。