Monogame C#:将图像分配到2D阵列吗? [英] Monogame C#: Assigning images to a 2D Array?
问题描述
因此,我尝试使用2D数组创建网格,我需要将图像分配给2D数组的每个点,但无法利用我目前的知识找到方法.我没有创建实际数组的问题,只需将图像分配给数组即可.
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.
推荐答案
如果我没记错的话,要实现的目标是使用定义的数组创建映射.如果是这样,可以采用以下方法:-首先,创建网格:
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,},
}
-接下来,在基于您在第1步中创建的网格的绘图中
- 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();
}
这篇关于Monogame C#:将图像分配到2D阵列吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!