2
\$\begingroup\$

I'm currently working on a project in XNA, Visual Studio, where it is crucial I can create the classic tetris figures solely based on one square sprite (So I can return to each individual square at a later point, as I need individual squares highlighted at another point in the game).

I tried loading my square into my code, then use a matrix(Texture2D 2d-array) to draw one of my pieces:

centerPiece = new Texture2D[,] { { null, Content.Load<Texture2D>("square"), null }, { Content.Load<Texture2D>("square"), Content.Load<Texture2D>("square"), Content.Load<Texture2D>("square") }, { null, Content.Load<Texture2D>("square"), null } };

At first glance it seemed fine, but when I try to actually draw it using this method:

spriteBatch.Draw(centerPiece[,], screenView(), null, Color.White, 0f, IMGCenter(), 1f, SpriteEffects.None, 0f);

It can't draw it as an 2D array, or normal array for that matter. Neither is it able to draw the "nulls" I use in my first method, which also denies me of drawing it by the use of a nested for loop.

Am I completely on the wrong track, or am I just missing a slight detail?

\$\endgroup\$
2
  • \$\begingroup\$ You would need to loop over each Texture2D in the array during the Draw(). You can't draw an array. \$\endgroup\$
    – lozzajp
    Commented Oct 21, 2016 at 10:24
  • \$\begingroup\$ @lozzajp This is what I meant I tried with my nested for loop, but it can't draw null. However I do need every "combined" sprite to be 3x3 for later simplicity for me, so some parts of the matrix has to be empty. Is the only way to work around this to create a 100% sprite, and how would I return and rotate a "combined" sprite when I've drawn it with a nested for loop? \$\endgroup\$
    – Jon Dier
    Commented Oct 21, 2016 at 10:27

1 Answer 1

3
\$\begingroup\$

Couple of things.

First and foremost you should only load your texture once like this:

Texture2D squareTex = Content.Load<Texture2D>("square");

And then you pass it into your array. There is no reason for the texture to occupy any more memory than that.

Secondly, no spritebatch does not take an array of textures - it takes a single one. So in your case you would loop over centerPiece like so:

foreach(Texture2D t in centerPiece) { 
    spriteBatch.draw(t, ...);
 }

Now, if you want to improve on this and save some memory you wouldn't store a bunch of textures but maybe an array of booleans instead and on every loop where the boolean is true you draw a texture. This would change the code to something like:

bool[,] shape = new boolean[,] {false, true, false, .....}
Texture2D tex = Content.Load<Texture2D>("square");

...

foreach(bool b in shape) {
    if(b) {
      spriteBatch.draw(tex, ...);
    }
}
\$\endgroup\$
9
  • \$\begingroup\$ Ah, this logic seems very reasonable. A few questions though, if I at a later state need to access this complete object, and i.e. rotate it, is there a way to do this, or should I unload and redraw it? \$\endgroup\$
    – Jon Dier
    Commented Oct 21, 2016 at 10:38
  • \$\begingroup\$ Whops, entered by mistake, also the for(bool b : shape) gives me a ";" exptected error \$\endgroup\$
    – Jon Dier
    Commented Oct 21, 2016 at 10:39
  • \$\begingroup\$ Well, if you're using this logic "rotating" would be setting certain bools to true and other to false. So your bool[,] would have to be big enough to accommodate all possible shapes and rotations. As to your error might have missed something else unrelated to my example sounds like you missed a ; somewhere. (edited to remove error) \$\endgroup\$
    – Mikael
    Commented Oct 21, 2016 at 10:48
  • \$\begingroup\$ Do I need to assign b somewhere else when using for each in stead of for? Getting 7 errors with this code: 'in' expected, ), ;, ; expected. Invalied expression form ')', Only assignment, call, etc. can be used as an assignment and The name 'b' doesn't exist in current context. Completely tried copying your code, but still error. \$\endgroup\$
    – Jon Dier
    Commented Oct 21, 2016 at 10:57
  • \$\begingroup\$ As a note all the errors are in either the for each (bool b : shape) or if(b) lines \$\endgroup\$
    – Jon Dier
    Commented Oct 21, 2016 at 10:58

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.