XNA (Maps from Text File)
Hello, I'm coming across a big problem while attempting to make an XNA game. I have a resource with a Text File loaded into it with my map using "+" as the walls, however I'm unable to get my code working right.
public Game1()
{
graphics = new GraphicsDeviceManager(this);
content = new ContentManager(Services);
// !!!MY CODE!!!
Assembly asm = Assembly.GetExecutingAssembly();
Stream stream = asm.GetManifestResourceStream(@"Game1.maps.map01.txt");
List<string> map = new List<string>();
StreamReader sr = new StreamReader(stream);
string line = sr.ReadLine();
while (line != null)
{
map.Add(line);
line = sr.ReadLine();
}
sr.Close();
stream.Close();
}
protected override void Draw(GameTime gameTime)
{
graphics.GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
// !!!MY CODE!!!
spriteBatch.Begin(SpriteBlendMode.AlphaBlend);
spriteBatch.Draw(gfx_MainPlane, spritePosition, Color.White);
for (int line = 0; line < maps.map01.Count; line++)
{
for (int column = 0; column < maps.map01[line].Length; column++)
{
switch (maps.map01[line][column])
{
case '+':
sp = gfx_MainWall;
break;
default:
sp = null;
}
if (sp != null)
{
spriteBatch.Draw(sp, new Vector2(column * 32, 32), Color.Black);
}
}
}
spriteBatch.End();
// !!!MY CODE!!!
base.Draw(gameTime);
}
}
}
I'm unable to get my Count and Length working right for my map.
for (int line = 0; line < maps.map01.Count; line++)
{
for (int column = 0; column < maps.map01[line].Length; column++)
{
switch (maps.map01[line][column])
{
case '+':
sp = gfx_MainWall;
break;
default:
sp = null;
}
if (sp != null)
{
spriteBatch.Draw(sp, new Vector2(column * 32, 32), Color.Black);
}
}
}
I also think I did this code wrong as well:
spriteBatch.Draw(sp, new Vector2(column * 32, 32), Color.Black);
I have never used Text files for maps before, if anyone can help me out that would be great!
[2196 byte] By [
cpaw88] at [2007-12-27]
I would recommend stepping through the code as it runs to see how things are being processed and what values are being thrown around, that always helps me out quite a bit.
Second, are you taking any errors compile or runtime? I don't see where gfx_MainWall is ever initialized, and could contain a null value itself. It could also be a problem with the Map class itself as there is no menton on how "map" is related to "maps". As for the count, make sure your map class starts at index 0 and not one (I tend to make that mistake often) as that can skew your numbers and throw errors. Finally, It's my understanding (and has worked in practice) that you want to use Color.White to draw an image unmodified. that being said Color.Black may blank out the image entirely.
Hope your able to glean something useful,
Dragoon.
A couple of things:
You are declaring a List<string> in your constructor, and it will not be available outside of your constructor because it will have gone out of scope. You would need to move this line:
List<string> map = new List<string>();
outside of any method for it to be available to the whole class. You can put it with the existing lines that say:
GraphicsDeviceManager graphics;
ContentManager content;That will let you access it from your Draw method.
In your draw method, the code says:
for (int line = 0; line < maps.map01.Count; line++)
{
for (int column = 0; column < maps.map01[line].Length; column++)
but there isn't anything called "maps" declared in the code you posted. If you move the List<string> map declaration outside of the constructor, this code in the draw method would be something like:
for (int line = 0; line < map.Count; line++)
{
for (int column = 0; column < maps[line].Length; column++)