-1

So I'm new to XNA and am attempting to draw multiple sprites to the screen in the easiest way. I want each sprite to be incremented in the X axis so that I am left with multiple sprites drawn across the screen.

I have defined:

Vector2 pos;

In the LoadContent function I have:

pos = new Vector2(0, 0);

and in Draw I have:

spriteBatch.Draw (circle, pos, Color.White); //Draws sprite to screen in correct position
spriteBatch.Draw(circle, (pos.X += 1), Color.White); //causes error and doesnt draw

Hopefully I've explained this well enough and you can see what I'm trying to do, the compiler doesn't agree with (pos.X += 50) (me trying to increment the X position by 50). I know I could go about this a longer way and create a new Vector2 for each draw but that would create multiple lines of what I think surely is unnecessary code and there must be a quick way like this to go about doing it?

pinckerman
  • 4,115
  • 6
  • 33
  • 42
Rich
  • 15
  • 1
  • 9

2 Answers2

4

The method signature of Draw expects the second parameter to be a Vector2, right?

If so, then (pos.X += 1) isn't a Vector2. It's a statement which increments the X parameter of the pos Vector2 variable, but the statement doesn't return an instance of a Vector2 object.

Edit: Code is as follows:

public void DrawSprites()
{
    // setup circle here
    // setup spritebatch here
    // setup initial pos here
    // setup MAX_ITERATIONS here

    var INCREMENT_VALUE = 50;

    for (var i = 0; i < MAX_ITERATIONS; i++) {
        var iteratedPos = pos + new Vector2((INCREMENT_VALUE * i), 0); // per Nikola's comment
        spriteBatch.Draw(circle, iteratedPos, 0), Color.White);
    }
}
pinckerman
  • 4,115
  • 6
  • 33
  • 42
Ryan Nigro
  • 4,389
  • 2
  • 17
  • 23
  • Thanks for the reply, the code you suggested makes the sprite fly across the screen in the X axis, I don't want it to do that, I want it to remain stationary and draw both sprites separately, one at the original coordinates and every one after that +50 pixels in the X axis so it just ends up as a line of duplicate sprites? – Rich Nov 24 '13 at 22:21
  • Ah. Set up a FOR loop and call spriteBatch.Draw from within that FOR loop. You'll have to instantiate a new Vector2 with your incremented X,Y on ever iteration of the loop. Make sense? – Ryan Nigro Nov 24 '13 at 22:23
  • Um, would you be able to write a simple code example? I understand the theory of that but implementing it into code is another story for me as a beginner!! (Sorry!) – Rich Nov 24 '13 at 22:32
  • Do you want all of the sprites to draw on a single call to your draw method, or should an additional sprite be drawn each time your method is called? – Ryan Nigro Nov 24 '13 at 22:33
  • all in a single call ideally – Rich Nov 24 '13 at 22:38
  • Ok so I tried what you said, it didn't work as I've obviously put things in completely the wrong place but can't paste the code here as this is a new account and there is a character limit to comments. http://pastebin.com/Urueep9P – Rich Nov 24 '13 at 22:55
  • See additional edits for clarity. I'm heading out after this. Good luck! – Ryan Nigro Nov 24 '13 at 22:59
1

You need to make a list of the draw rectangles of your textures.

//implement Texture2D (called "image") and SpriteBatch (called "spriteBatch")
List<Rectangle> rectangles = new List<Rectangle>();
const int ITERATIONS = 10; //or whatever you want the iterations to be
const int INCREMENT_VALUE = 50; //again, whatever you want it to be

for (int i = 0; i < ITERATIONS; i++)
{
    for (int j = 0; j < rectangles.Count; j++)
    {
        rectangles[j].X += INCREMENT_VALUE * i;
        spriteBatch.Draw(image, rectangles[j], Color.White);
    }
}

Like said earlier, you need a list of Rectangles that have all the image rectangles in them. Hope I helped, and good luck!

Daniel G
  • 245
  • 4
  • 15