1 Replies - 660 Views - Last Post: 20 December 2016 - 05:35 AM

#1 WONDERGG  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-October 16

Placing sprites properly to form an object

Posted 24 October 2016 - 12:18 AM

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.

Is This A Good Question/Topic? 0
  • +

Replies To: Placing sprites properly to form an object

#2 SixOfEleven  Icon User is offline

  • Planeswalker
  • member icon

Reputation: 1055
  • View blog
  • Posts: 6,643
  • Joined: 18-October 08

Re: Placing sprites properly to form an object

Posted 20 December 2016 - 05:35 AM

What you are trying to achieve is a basic tile engine. Instead of using a foreach I would use nested for loops. Something like this, it will take tweaking on your end to get it right.

Vector2 position = new Vector2(100, 100);
int imageWidth = 20;
int imageHeight = 20;

for (int y = 0; y < height_of_matrix; y++)
{
    for (int x = 0; x < width_of_matrix; x++)
    {
        if (matrix[x, y])
        {
            Vector2 drawAt = position;
            drawAt.X += x * imageWidth;
            drawAt.Y += y * imageHeight;

           // Draw the image using the drawAt vector2
        }
    }
}


This post has been edited by SixOfEleven: 20 December 2016 - 05:36 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1