【问题标题】:How to declare an array of objects C#如何声明一个对象数组C#
【发布时间】:2013-08-20 04:02:28
【问题描述】:

我正在为游戏制作 2D 瓷砖地图。我有一个单元格类(瓦片)使单元格对象具有 4 个属性:TopWall、BottomWall、LeftWall、RightWall。墙可能存在也可能不存在,因此这些属性是布尔值 true 或 false,如果它们为 true,则会在该单元格墙上画一条线(不允许玩家穿过单元格)。我想声明一个名为 Map1 的单元格对象的(二维?如行和列)数组(因为它们将组成一个游戏地图)。然后我想将数组的每个成员设置为具有特定的墙属性。这是我所拥有的:

Cell.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Map
{
    public class Cell : PictureBox
    {
        bool LeftWall; 
        bool TopWall; 
        bool RightWall;
        bool BottomWall;

        public Cell(bool TopWall, bool RightWall, bool BottomWall, bool LeftWall)
        {
            this.TopWall = TopWall;
            this.RightWall = RightWall;
            this.BottomWall = BottomWall;
            this.LeftWall = LeftWall;
        }
    }
}

这是我开始尝试制作单元格对象数组并设置墙属性: Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Map
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        /// <summary>
        /// Array of tiles
        /// </summary>
        //PictureBox[,] Boxes = new PictureBox[4,4];
        public void initiateArray()
        {
            Cell[,] Map1 = new Cell[4, 4];

            for (int row = 0; row < 4; row++)
            {
                for (int column = 0; column < 4; column++)
                {
                    Map1[row, column] = new Cell();
                }
            }

            Map1[0, 0] = new Cell(true, false, false, true);
            Map1[0, 1] = new Cell(false, false, false, true);
        }


      /*int [][] Walls = new int[][] {
      new int[] {0,0}, new int[] {1,0,0,1},
      new int[] {1,0}, new int[] {1,0,1,0},
      new int[] {0,1}, new int[] {0,0,0,1}, 
      new int[] {1,1}, new int[] {1,1,1,0}, 
      new int[] {2,0}, new int[] {1,1,0,0},
      new int[] {2,1}, new int[] {0,0,0,1}, 
      new int[] {3,1}, new int[] {1,0,1,0}, 
      new int[] {0,2}, new int[] {0,0,1,1}, 
      new int[] {1,2}, new int[] {1,0,1,0}, 
      new int[] {2,2}, new int[] {0,1,1,0}};*/



        #region Runtime load
        /// <summary>
        /// Build the map when window is loaded
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Form1_Load(object sender, EventArgs e)
        {
            BuildMap();
        }
        #endregion

        #region Build grid
        /// <summary>
        /// Draw every tile on the map
        /// </summary>
        public void BuildMap()
        {
            for (int row = 0; row < 3; row++)
            {
                for (int column = 0; column < 3; column++)
                {
                    DrawCell(row, column);
                }
            }
            //draw the exit box
            DrawCell(1, 3);
        }
        #endregion

        #region Draw
        /// <summary>
        /// Draw one tile
        /// </summary>
        /// <param name="row"></param>
        /// <param name="column"></param>
        public void DrawCell(int row, int column)
        {
            Map1[row, column] = new Cell();
            Map1[row, column].Height = 100;
            Map1[row, column].Width = 100;
            Map1[row, column].BorderStyle = BorderStyle.FixedSingle;
            Map1[row, column].BackColor = Color.BurlyWood;
            Map1[row, column].Location = new Point((Map[row, column].Width * column), (Map[row, column].Height * row));
            this.Controls.Add(Map1[row, column]);

           // System.Drawing.Pen myPen;
           // myPen = new System.Drawing.Pen(System.Drawing.Color.Red);
            //System.Drawing.Graphics formGraphics = this.CreateGraphics();


            //formGraphics.DrawLine(myPen, 0, 0, 200, 200);
            //myPen.Dispose();
            //formGraphics.Dispose();
        }



        public void DrawWalls(int row, int column)
        {

        }
        #endregion
    }


}

它显示了很多错误,我想知道是否有人能看到我哪里出错了。谢谢。

【问题讨论】:

  • 有很多,但我想知道的是,当您将鼠标悬停在 Map1 上时,它会显示“命名空间不能直接包含字段和方法等成员”。在它说 Map1 被用作一种类型之前,它不是。希望有人可以看到代码并看到它显然是错误的并指明方向。与Map1有关
  • 最重要的问题 - 您必须将代码放入方法中。它不能在班级级别上。
  • @MarcinJuraszek 谢谢刚刚更新它,但我尝试在我成功后立即调用新方法,它不会让我这样做。它还说 Map1 在当前上下文中不存在。
  • StackOverflow 颜色可能有点偏,但对我来说,您似乎正试图从外部范围访问方法变量。您不能从包含方法的上下文之外访问 Map1 变量。您是否尝试将其设为 Form1 中的公共属性?

标签: c# arrays object


【解决方案1】:

首先确保 PictureBox 实现了非参数化构造函数。

第二和第三Map1[i] = new Cell();你初始化了一个矩阵,你需要两个嵌套循环来遍历整个事物,你需要使用逗号符号[row, col]来访问它。你在循环中使用 row not i 。

第四个你在末尾缺少一个“新”Map1[0,0] = Cell(true, false, false, true);

【讨论】:

  • 谢谢。 ` for (int row = 0; row
【解决方案2】:

常见错误。 Cell[][]Cell[,] 不同。

还有

var foo = new Cell[4,4];
Console.WriteLine(foo.Length);

按照您的代码假设,给出 16 而不是 4。

如果你想使用数组的“长度”信息,你应该这样做。

    public void initiateArray()
    {
        Cell[,] Map1 = new Cell[4, 4];

        for (int row = 0; row < Map1.GetLength(0); row++)
        {
            for (int column = 0; column < Map1.GetLength(1); column++)
            {
                Map1[row, column] = new Cell();
            }
        }

        Map1[0, 0] = new Cell(true, false, false, true);
        Map1[0, 1] = new Cell(false, false, false, true);
    }

或者(因为我完全不相信自己在 Off By One 错误中的能力,使用 Linq)

    public void initiateArray()
    {
        Cell[,] Map1 = new Cell[4, 4];

        foreach(var row in Enumerable.Range(0, Map1.GetLength(0)))
        foreach(var column in Enumerable.Range(0, Map1.GetLength(1)))
        {
                Map1[row, column] = new Cell();         
        }
        Map1[0, 0] = new Cell(true, false, false, true);
        Map1[0, 1] = new Cell(false, false, false, true);
    }

【讨论】:

  • 谢谢,我想要 16 个单元格。我想要一个 4x4 网格。我应该使用 [][]?
  • 我的意思是你最初是在迭代int row = 0 to 15(混合我的VB/C#)。当row &gt;= 4 时,这显然会给你一个IndexOutOfRangeException
  • 谢谢。我已经复制并粘贴了您发布的第二种方法。我在 Form1 类的构造函数中调用了 InitiateArray。有没有办法可以打印 map1 的内容来检查 Map1 数组中的两个 Cell 成员是否正确?我对 C# 非常陌生,只熟悉 JavaScript HTML5 画布应用程序
  • 当然你可以使用Console.WriteLine() 但是我建议你学习使用VS调试器。只需敲入断点并在调试模式下运行代码。当程序暂停时,您会看到 Local/Watcher 窗口,您可以将鼠标悬停在任何变量上以查看里面的内容。查找有关如何调试 .net 的教程。
猜你喜欢
  • 1970-01-01
  • 2017-01-17
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-11-17
  • 2011-05-10
  • 1970-01-01
相关资源
最近更新 更多