1

So I'm trying to create a grid using a 2D array, I need to assign images to each point of the 2D array but can't find a way with the knowledge I currently have. I have no issue creating the actual array, just assigning the image to the array.

    mark = Content.Load<Texture2D>("mark");
    peep1 = Content.Load<Texture2D>("peep1");
    peep2 = Content.Load<Texture2D>("peep2");
    peep3 = Content.Load<Texture2D>("peep3");


    int[,] grid = new int[6, 6];

    grid[0, 0] = peep1; 

I have tried assigning the image in multiple ways, shown above was my first attempt as it is what I had saved. Sorry if this is really obvious, I'm still new.

3 Answers3

1

Not sure what your exact requirements are but you can do it like this:

mark = Content.Load<Texture2D>("mark");
peep1 = Content.Load<Texture2D>("peep1");
peep2 = Content.Load<Texture2D>("peep2");
peep3 = Content.Load<Texture2D>("peep3");


Texture2D[,] grid = new Texture2D[6, 6];

grid[0, 0] = peep1; 

Simply change the data type from int to Texture2D since you're assigning a Texture2D instead of an int anyways.

jegtugado
  • 5,081
  • 1
  • 12
  • 35
  • And how would I get this to draw as part of a grid? Sorry I'm really struggling to get the hang of monogame – T_McMillan97 Jan 15 '19 at 01:47
  • 1
    You can loop through your grid and draw each `Texture2D` using `SpriteBatch`. It would be a lot easier to create a game if you know object-oriented programming such as creating models through classes and such. It is VERY difficult to explain it here as an answer but you should study about it as it is very useful. – jegtugado Jan 15 '19 at 01:52
1

If I am not mistaken, what you want to achieve is that you want to create a map with the array you define. If so, here is a way to do this: - First of all, create the grid:

int[,] grid = new int[,]
{
    //just 2x2 grid, for example
    {0, 1,},
    {1, 2,},
}

- Next, in draw based on the grid you created in step 1:

public void Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Begin();
    for (int i = 0; i < grid.GetLength(1); i++)//width
    {
        for (int j = 0; j < grid.GetLength(0); j++)//height
        {
            int textureIndex = grid[j, i];
            if (textureIndex == -1)
                continue;

            Texture2D texture = tileTextures[textureIndex];//list of textures
            spriteBatch.Draw(texture, new Rectangle(
                i * 60, j * 60, 60, 60), Color.White);
        }
    }
    spriteBatch.End();
}
Silver
  • 482
  • 2
  • 6
0

if you actually want to draw these in a grid then you should call one of the DRAW methoed, and grant it the postion that you want to assign the textures to.. you should create an array of points that you wnat to draw the textures to, and use it in the darw method. use vector or point:(lets say its 60X60 pixels)

    markpoint = new Point (0,0);
    peep1pont = new Point (60,0);
    peep2point = new Point (0,60);
    peep3point = new Point (60,60);

for i to numberOfTextures:
draw(...,...,Texture(the array or grid of textures),Point(the array or grid of points),...,...)
Ben Efrat
  • 11
  • 5
  • So I think I have this right. I create a variable for each image as usual, so peep1 and peep2 for example. Then create a point array and assign each one to a point in the array? Something like this? – T_McMillan97 Jan 23 '19 at 14:08
  • yeah, that's right..it is a simple solution and straightforward to your specific problem.if you wish some more complex solutions you should check this out[Tiles](http://www.alienscribbleinteractive.com/Tutorials/tile_map_tutorial.html) – Ben Efrat Jan 23 '19 at 18:55