0
\$\begingroup\$

I'm working with XNA in Visual Studio, and I'm trying to draw this sprite: https://i.imgur.com/Dy5nSuG.png

Only using this sprite: https://i.imgur.com/6T8TeTQ.png

My current attempt relies on a matrix boolean, which then gets drawn in a foreach loop:

//Variable
 bool[,] centerPiece = new bool[,] { { false, true, false }, { true, true, true }, { false, true, false } };

//Loop
 foreach (bool b in centerPiece)
            {
                if (b == true)
                {
                    spriteBatch.Draw(squareTex, screenView(), null, Color.White, 0f, IMGCenter(), 1f, SpriteEffects.None, 0f);
                }
            }

While this draws all 5 squares, it doesn't place them as desired (because of they're all being drawn in the screenView() method placement(The center of the screen):

public Vector2 screenView()
        {
            Viewport viewport = GraphicsDevice.Viewport;
            Vector2 screenCenter = new Vector2(viewport.Width / 2f, viewport.Height / 2f);
            return screenCenter;
        }

How do I modify my loop to place the squares correctly, and/or do I need to change my implementation completely to achieve my desired goal?

Note: I need to both be able to access the object as a whole, and also access each individual square, in each individual object.

\$\endgroup\$
0
\$\begingroup\$

This data representation of your game is fine:

//Variable
 bool[,] centerPiece = new bool[,] { { false, true, false }, { true, true, true }, { false, true, false } };

The next bit of code you supplied is being run during your Draw phase of your update loop. Here you are using a foreach loop. This is bad as it will generate a lot of garbage. You are going to want to use a for loop instead.

This will also provides you with the x,y coordinate of the data you are attempting to draw. You can then use this to provide the offset when drawing your square:

float Spacing = 1f;     //Do you need to adjust the spacing?

//Loop through your data
for (int x = 0; x < centerPiece.GetLength(0); x++) {
    for (int y = 0; y < centerPiece.GetLength(1); y++) {

        if (centerPiece[x,y] == true) {

            //Determine the position you will be drawing this square
            Vector2 offset = new Vector2(x,y) * Spacing;
            Vector2 position = IMGCenter() + offset;

            spriteBatch.Draw(squareTex, screenView(), null, Color.White, 0f, position, 1f, SpriteEffects.None, 0f);
        }
    }
}

You could take it a step further, and create a Tile class that you initialize. You could then specify its position, size, color, etc.. Then instead of a boolean array, you would create a Tile object array, which would already have everything pre-calculated and ready to go.

You would then create a Draw() and Update() method for this Tile class.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks for the reply! A few questions. 1: In the foreach loop I initizalize the bool b, I can't do that with these for loops. 2: Wouldn't this only make me able to draw in one direction at the time? I'll need to rotate these "combined" sprites at a later point, so just want to make sure this is the way to go. Kind regards \$\endgroup\$
    – Jon Dier
    Oct 24 '16 at 11:36
  • \$\begingroup\$ #1. Removed the reference to b -- replaced it with reference to array (See code above). \$\endgroup\$
    – jgallant
    Oct 24 '16 at 11:37
  • \$\begingroup\$ #2. It will not no -- it will draw the sprites in relation to the ScreenCenter point that you defined. The offset will be calculated based on the position of each tile in its array (the x, y index). If you need to rotate them as a group of sprites, that is a different problem. \$\endgroup\$
    – jgallant
    Oct 24 '16 at 11:38

You must log in to answer this question.

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