Monogame C#:将图像分配给2D阵列?

问题描述 投票:0回答:3

所以我正在尝试使用2D数组创建一个网格,我需要将图像分配给2D数组的每个点,但是找不到具有我当前知识的方法。我没有创建实际数组的问题,只是将图像分配给数组。

    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; 

我尝试过以多种方式分配图像,上面显示的是我的第一次尝试,因为它是我保存的内容。对不起,如果这很明显,我还是新人。

c# arrays multidimensional-array monogame
3个回答
1
投票

不确定您的具体要求是什么,但您可以这样做:

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; 

只需将数据类型从int更改为Texture2D,因为无论如何都要分配Texture2D而不是int


0
投票

如果你真的想在网格中绘制这些,那么你应该调用其中一个DRAW方法,并给它你要分配纹理的位置..你应该创建一个点数组,你可以绘制纹理到,并在darw方法中使用它。使用矢量或点:(比方说它的60X60像素)

    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),...,...)

0
投票

如果我没有弄错的话,你想要实现的是你想用你定义的数组创建一个地图。如果是这样,这是一种方法: - 首先,创建网格:

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

- 接下来,在基于您在步骤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();
}
© www.soinside.com 2019 - 2024. All rights reserved.