2013-08-20 46 views
1

我正在为一个游戏制作二维瓷砖地图。我有一个单元类(瓷砖)使单元对象具有4个属性:TopWall,BottomWall,LeftWall,RightWall。墙可能会或可能不会在那里,所以这些属性是布尔真或假,如果它们是真的,一条线将被拉下该单元格墙(不允许玩家穿过单元格)。我想声明一个名为Map1的单元对象(2维?作为in,row和column)数组(因为它们将组成游戏地图)。然后我想设置数组的每个成员具有特定的墙属性。这里是我有:如何声明一个对象数组C#

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 
    } 


} 

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

+0

显示你的错误。 – MarcinJuraszek

+0

有很多,但我想知道的是,当你将鼠标悬停在Map1上时,它说“一个名称空间不能直接包含成员,如字段和方法”。而之前它说Map1被用作一种类型时,它不是。希望有人可以看到代码,并看到它显然是错误的,并显示出路。这是与Map1 – user2602079

+0

最重要的问题 - 你必须把你的代码放入方法。它不能在课堂上进行。 – MarcinJuraszek

回答

2

首先确保PictureBox有一个非参数化的构造函数。

第二和第三Map1[i] = new Cell();您初始化了一个矩阵,您需要两个嵌套循环来遍历整个事物,并且您需要通过使用逗号表示法[row, col]访问它。你在你的循环中不使用行。

四你在年底Map1[0,0] = Cell(true, false, false, true);

+0

谢谢。 for(int row = 0; row user2602079

1

常见错误缺少一个“新”。 Cell[][]Cell[,]不一样。

而且

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

给出16和NOT 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); 
    } 

或(因为我有我自己的关能力绝对没有信心通过一个错误,使用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); 
    } 
+0

谢谢,我确实需要16个单元格。我想要一个4x4网格。我应该使用[] []? – user2602079

+0

我的意思是,你最初是迭代'int row = 0到15'(混合我的VB/C#)。当'row> = 4'时显然会给你一个'IndexOutOfRangeException'。 – Aron

+0

谢谢。我已复制并粘贴您发布的第二种方法。我在Form1类的构造函数中调用了InitiateArray。有没有办法可以打印map1的内容来检查Map1数组中的两个Cell成员是否正确?我对C#非常陌生,只对JavaScript非常熟悉HTML5 canvas应用程序 – user2602079