-
November 7th, 2013, 03:47 AM
#1
A little array issue
So I'm pretty sure all this is syntactically correct. What I've done is create a struct object to used as an array on another object. (An array of objects within an object if you will)
What I have here is this
Code:
public struct Tile
{
private const int MAXLAYERS = 4;
private const int MAXDATA = 5;
private int tileAttribute;
private static int[] tileLayer = new int[MAXLAYERS];
private static object[] tileData = new object[MAXDATA];
//this method is used to update the tile's graphic, given a specific layer
public void updateTile(int layer, int tile)
{
tileLayer[layer] = tile;
}
// overloaded updateTile, allowing you to update just the attribute
public void updateTile(int attribute)
{
tileAttribute = attribute;
}
//overloaded updateTile, allows you to update the tileData of a tile, given a dataIndex
public void updateTile(int dataIndex, object newData)
{
tileData[dataIndex] = newData;
}
public int getTileLayer(int layer)
{
return tileLayer[layer];
}
public int getTileAttribute()
{
return tileAttribute;
}
public object getTileData(int dataIndex)
{
return tileData[dataIndex];
}
public int getMaxLayers()
{
return MAXLAYERS;
}
public int getMaxData()
{
return MAXDATA;
}
}
public class Map
{
private const int MAXMAPX = 20;
private const int MAXMAPY = 15;
public Tile[,] mapTile = new Tile[MAXMAPX, MAXMAPY];
// Messy blank map ctor
public Map()
{
// nested for loops to make all tiles blank
for (int x = 0; x <= MAXMAPX - 1; x++)
{
for (int y = 0; y <= MAXMAPY -1; y++)
{
for (int z = 0; z <= mapTile[x,y].getMaxLayers() - 1; z++)
{
mapTile[x, y].updateTile(z, 0);
}
}
}
// nested for loops to make all map data blank
for (int x = 0; x <= MAXMAPX - 1; x++)
{
for (int y = 0; y <= MAXMAPY - 1; y++)
{
for (int z = 0; z <= mapTile[x, y].getMaxData() - 1; z++)
{
mapTile[x, y].updateTile(z, "");
}
}
}
// for loop to make all attributes blank
for (int x = 0; x <= MAXMAPX - 1; x++)
{
for (int y = 0; y <= MAXMAPY - 1; y++)
{
mapTile[x, y].updateTile(0);
}
}
}
}
blah blah, all chipper. I think I've done all that correctly. Anyhow, now I'm trying to test out my objects and I have this in the main method
Code:
static void Main()
{
Map testMap = new Map();
testMap.mapTile[1, 0].updateTile(0,1);
Console.WriteLine(testMap.mapTile[0, 0].getTileLayer(0) + "\n" + testMap.mapTile[1, 0].getTileLayer(0));
}
Now, what I assume that does is find that tile using the index and calls that method passing that data by. However, when I print an unedited one, and the one I deliberately edited, the output I get is
I hope someone can understand why I'm stumped. I've been stuck for hours and googleing this hasn't been very easy, but thankfully, I was able to find this forum.
I hope someone can offer guidance! Also, if there's anything I can do as far as optimization goes, that'd be great. I imagine I'm doing this in a very poor manner...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|