2
\$\begingroup\$

So I am trying to make more than one sprite using a single class but I cannot seem to get the sprites to be drawn at all or drawn in different places (they are all just on top of one another).

In my Game1 I have:

    Flag flag;
    List<Flag> GameFlags = new List<Flag>();

then in LoadContent:

flag = new Flag();
tempTexture = Content.Load<Texture2D>("FlagTexture");
tempRectangle = new Rectangle(50, 500,tempTexture.Width / 3, tempTexture.Height / 3);
flag.SetSprite(tempTexture);
flag.SetRectangle(tempRectangle);

for (int i = 0; i < 2; i++)
        {
            GameFlags.Add(flag);
            flag.spriteRectangle.X += 100;
        }

Then in Draw:

 foreach (Flag flag in GameFlags)
        {
            spriteBatch.Begin();
            flag.Draw(spriteBatch);
            spriteBatch.End();
        }

The effect I am after is having 2 sprites spaced out by a specified amount of space. Image if the underscores were spacing and the zeros were sprites in the following:

0____________________________0

Any help and advice is appreciated.

Thanks in advance,

TeaBelliedTitch.

\$\endgroup\$
1
  • \$\begingroup\$ You should move your spriteBatch.Begin() and spriteBatch.End() calls out of the loop. You only need to call them once in this context. Put begin before the loop and end after. \$\endgroup\$
    – Keeleon
    Commented Mar 19, 2016 at 0:39

1 Answer 1

1
\$\begingroup\$
for (int i = 0; i < 2; i++)
    {
        GameFlags.Add(flag);
        flag.spriteRectangle.X += 100;
    }

Is this not adding a reference to the same object? So you have a list of references to one thing as opposed to a list of things or references to things

\$\endgroup\$
1
  • \$\begingroup\$ Yes, they are going to want new Flag()s at the start of each loop iteration. \$\endgroup\$
    – lozzajp
    Commented Mar 2, 2017 at 16:52

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .