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\$
  • \$\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 Mar 19 '16 at 0:39
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\$
  • \$\begingroup\$ Yes, they are going to want new Flag()s at the start of each loop iteration. \$\endgroup\$ – lozzajp Mar 2 '17 at 16:52

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.