Ryan

Ryan

  • NA
  • 2
  • 0

Help Request: List and objects

Sep 8 2009 6:05 PM

I have a form object that makes 30 new star objects when it constructs and stores them in a List collection, then on its paint event it calls a renderer object's method to draw itself. Inside that method it loops through the stars in the list and draws them.

The only trouble is that all the stars in the list end up referencing only ONE star, so only one gets draw (30 times).

And the funny thing is that if I put a stop (not sure about the technical name) so the debugger stops on the line where it starts making the stars (inside of the form constructor) and I walk it through making all the stars.. it works just fine.. and has a whole list of 30 separately referenced stars...


The form class:
public partial class fieldForm : Form { public Renderer renderer; public List stars = new List(); public fieldForm() { InitializeComponent(); Random random = new Random(); for (int i = 0; i < 30; i++) { Star newStar = new Star(this, random); stars.Add(newStar); } } private void fieldForm_MouseClick(object sender, MouseEventArgs e) { MessageBox.Show(e.Location.ToString()); } private void fieldForm_Paint(object sender, PaintEventArgs e) { renderer.PaintField(e.Graphics); } }

And this is just part of the renderer class
public class Renderer { private World world; private hiveForm HiveForm; private fieldForm FieldForm; private int timeOfDay = 0; private int adder = 0; private Bitmap holder = Properties.Resources.sunmoon; private int shaderAlpha = 0; private int shaderCounter = 6500; private int shaderAdder = 0; public Renderer(World World, hiveForm hiveForm, fieldForm fieldForm) { this.world = World; this.HiveForm = hiveForm; this.FieldForm = fieldForm; Random random = new Random(); InitializeImages(); } public void PaintField(Graphics g) { using (Pen brownPen = new Pen(Color.Brown, 6.0F)) { g.FillRectangle(Brushes.SkyBlue, 0 ,0 ,FieldForm.ClientSize.Width, FieldForm.ClientSize.Height / 2); //g.FillEllipse(Brushes.Yellow, new RectangleF(50, 15, 70, 70)); #region star drawing foreach (Star star in FieldForm.stars) { //lots of star drawing code } } } }

And I'm sorry about the code all being together.... if someone can tell me what html tags or formatting I have to do to unsquish it, I'd be glad to edit this post..


Answers (1)