1

ゲーム用の 2D タイル マップを作成しています。I Have a Cell クラス (タイル) は、TopWall、BottomWall、LeftWall、RightWall の 4 つの属性を持つセル オブジェクトを作成します。壁が存在する場合と存在しない場合があるため、これらの属性はブール値の true または false であり、true の場合、セルの壁に沿って線が引かれます (プレイヤーがセルを通過することはできません)。Map1 というセル オブジェクトの (2 次元? 行と列のように) 配列を宣言したいと思います (これらはゲーム マップを構成するため)。次に、配列の各メンバーを特定の壁属性に設定したいと思います。ここに私が持っているものがあります:

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
    }


}

多くのエラーが表示されており、どこが間違っているのか誰かがわかるかどうか疑問に思っていました。ありがとう。

4

2 に答える 2

1

よくある間違い。Cell[][]と同じではありませんCell[,]

また

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

あなたのコードが仮定したように、4ではなく16を与えます。

配列の「長さ」情報を使用する場合は、このようにする必要があります。

    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を使用して、Off By Oneバグで自分の能力をまったく信じていないため)

    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);
    }
于 2013-08-20T04:17:25.007 に答える