How to declare an array of objects C# -
i'm making 2d tile map game. have cell class (tiles) makes cell objects have 4 attributes: topwall, bottomwall, leftwall, rightwall. wall may or may not there, attributes boolean true or false, , if true, line drawn down cell wall (not allowing player pass through cell). want declare (2 dimensional? in, row, , column) array of cell objects called map1 (as make game map). want set each member of array having specific wall attributes. here have:
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; } } }
this me starting attempt making array of cell objects , set wall attributes: 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]; (int row = 0; row < 4; row++) { (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 map when window 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 map /// </summary> public void buildmap() { (int row = 0; row < 3; row++) { (int column = 0; column < 3; column++) { drawcell(row, column); } } //draw exit box drawcell(1, 3); } #endregion #region draw /// <summary> /// draw 1 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 } }
it showing lot of errors , wondering if can see i'm going wrong. thanks.
first make sure picturebox has non-parameterized constructor implemented.
second , thrid map1[i] = new cell();
initialized matrix, need 2 nested loops traverse whole thing , need access using comma notation [row, col]
. use row not in loop.
fourth missing 'new' @ end map1[0,0] = cell(true, false, false, true);
Comments
Post a Comment