3

Possible Duplicate:
Random number generator not working the way I had planned (C#)

Night, guys. Once again I'm having some trouble with mah code, yep.

This is the case, I got a list of objects of the class Star (white dots). I've overriden the Draw method so through a random value (from a static function) it should pick some stars and make 'em black for a short period of time, just to give that flickering effect.

The problem is, that all the stars go black at the same time, as they come back to normal... at the same time! Here is my code:

Stars = new List<Star>();
for (int x = 0; x < 90; x++)
     Stars.Add(new Star(rnum(1024, rnumber), rnum(768, rnumber), pixel, 0.25f, 1));
for (int x = 0; x < 40; x++)
     Stars.Add(new Star(rnum(1024, rnumber), rnum(768, rnumber), pixel, 1f, 2f));
for (int x = 0; x < 10; x++)
     Stars.Add(new Star(rnum(1024, rnumber), rnum(768, rnumber), pixel, 1.5f, 3));

That's declaring. Parameters in order are X, Y, texture, speed and size.

for (int x = 0; x < Stars.Count; x++)
     Stars[x].Draw(spriteBatch);

That's for drawing (I've also tried foreach)

 if (blackCounter > 0)
 {
      spBatch.Draw(texture, position, new Rectangle(framePosition * (int)frameSize.X, 0, (int)frameSize.X, (int)frameSize.Y), Color.Black);
      blackCounter--;
 }
 else
 {
      if (rNumber(20,rn) == 5)
      blackCounter += rNumber(100,rn);
      base.Draw(spBatch);
 }

This is the overriden Draw method for the stars. blackCounter should go to a value from 0 to 100 in 1 of 20 cases, and then slowly go down, while painting the star black.

Here's a screenshot, sigh...

a busy cat http://img444.imageshack.us/img444/2930/clipboard01zy.png

Community
  • 1
  • 1
ZShock
  • 255
  • 3
  • 12
  • would your `rnum` / `rNumber` do a `new Random()` by any chance? – Marc Gravell Dec 01 '11 at 07:34
  • 1
    if so, see http://stackoverflow.com/questions/767999/random-number-generator-not-working-the-way-i-had-planned-c and http://stackoverflow.com/questions/4479592/c-sharp-getting-the-same-random-number-repeatedly – Marc Gravell Dec 01 '11 at 07:36
  • I've tried three methods, new Random, using a Random as parameter and using a Random but not as a parameter. Everything leads to the same. – ZShock Dec 01 '11 at 07:37
  • Actually I was creating a new Random for every object, like an attribute. Tried using a universal one as parameter from the main Game class and everything went okay, d'ough! Thanks for your help. – ZShock Dec 01 '11 at 07:44

0 Answers0