【问题标题】:Adding multiple elements to a single line of a List将多个元素添加到列表的单行
【发布时间】:2020-11-26 14:23:48
【问题描述】:

您好,我正在尝试将坐标集添加到列表的单行(因此 x,y 作为单行列表)。当我分解图像并在网格中显示块时,我目前正在生成 x 和 y 坐标。能够将 x 和 y 添加到同一 List 行中,我遇到了障碍。这是我到目前为止的代码 -

    float sf = 1f;
    int x = 0;
    int y = 0;
    int width = tileImage.Width / tileNumber;
    int height = tileImage.Height / tileNumber;
    int placeXValue = (width / 10);
    int placeYValue = (height / 10);
    int placeX = placeXValue;
    int placeY = placeYValue;
    Rectangle tileRect = tileImage.Bounds;
    tileRect.Width = width;
    tileRect.Height = height;
    coordinates = new List<int>();

    for (int i = 0; i < tileNumber; i++)
    {
        for (int j = 0; j < tileNumber; j++)
        {
            tileRect.X = x;
            tileRect.Y = y;
            _spriteBatch.Draw(tileImage, new Vector2((x + placeX) * sf, (y + placeY) * sf), tileRect, Color.White, 0, new Vector2(0, 0), sf, SpriteEffects.None, 0);
            placeX += placeXValue;
            x += width;
            coordinates.Add(x,y);
        }
        x = 0;
        y += height;
        placeX = placeXValue;
        placeY += placeYValue;
    }

【问题讨论】:

  • 您可能需要coordinates.Add(new Coordinate(x, y)) 之类的东西,但在不知道您的坐标列表是什么的情况下,我们无法确定。请edit您的问题包括坐标列表的声明。主要问题是您试图为仅接受 1 的方法提供 2 个参数
  • 使用坐标 = new List();
  • 添加了我如何解决我遇到的问题的答案

标签: c# list monogame


【解决方案1】:

我发现了如何做我在问题中尝试做的事情。我做的第一件事是将我的 - private List&lt;int&gt; coordinates; - 更改为这样的元组列表 - List&lt;Tuple&lt;int, int&gt;&gt; coordinates; - 然后允许我使用 - coordinates.Add(new Tuple&lt;int, int&gt;(x,y)); - 元组功能将我的 x 和 y 元素添加到新列表中.进行此更改后,我可以将生成的坐标对添加到列表中。

【讨论】:

    猜你喜欢
    • 2020-08-19
    • 2016-02-29
    • 2012-09-11
    • 1970-01-01
    • 1970-01-01
    • 2020-02-17
    • 2015-03-07
    • 2017-04-18
    • 1970-01-01
    相关资源
    最近更新 更多