2013-08-20 108 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