• Advertisement
Sign in to follow this  

[XNA] Drawing List of Strings

This topic is 2147 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey!

I'm creating a ScreenManager, and essentially, I'd like to have the only menu items be strings instead of textures. When I run my program, it shows a clutter of text which doesn't look right at all. If you have any suggestions, please feel free to let me know:

MainMenu.cs:
[source lang="csharp"]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace MyRTS
{
class MainMenu : ScreenManager
{
public MainMenu()
{
Items = new List<string>();
Items.Add("New Game");
Items.Add("Exit Game");

Position = new List<Vector2>();
Position.Add(new Vector2(0, 25));
Position.Add(new Vector2(0, 50));
}

public void LoadContent(ContentManager Content)
{
Font = Content.Load<SpriteFont>("MainMenuFont");
}
}
}
[/source]

ScreenManager.cs
[source lang="csharp"]
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace MyRTS
{
class ScreenManager
{
private Texture2D texture;
private List<string> items;
private SpriteFont font;
private List<Vector2> position;
private Color color;

public Texture2D Texture
{
get { return texture; }
set { texture = value; }
}

public List<string> Items
{
get { return items; }
set { items = value; }
}

public SpriteFont Font
{
get { return font; }
set { font = value; }
}

public List<Vector2> Position
{
get { return position; }
set { position = value; }
}

public void Update()
{
}

public void Draw(SpriteBatch spriteBatch)
{
foreach (string item in items)
foreach (Vector2 pos in position)
spriteBatch.DrawString(font, item, pos, Color.White);
}
}
}
[/source]

Game1.cs
[source lang="csharp"]
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace MyRTS
{
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

MainMenu mainMenu = new MainMenu();

public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
}

protected override void Initialize()
{
base.Initialize();
}

protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);

mainMenu.LoadContent(Content);
}

protected override void UnloadContent()
{
}

protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

mainMenu.Update();

base.Update(gameTime);
}

protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);

spriteBatch.Begin();

mainMenu.Draw(spriteBatch);

spriteBatch.End();

base.Draw(gameTime);
}
}
}
[/source]

Share this post


Link to post
Share on other sites
Advertisement
The problem is this:

public void Draw(SpriteBatch spriteBatch)
{
foreach (string item in items)
foreach (Vector2 pos in position)
spriteBatch.DrawString(font, item, pos, Color.White);
}


This draws each string to each of the positions, which isn't the way you want it. Try this:


public void Draw(SpriteBatch spriteBatch)
{
for(int i = 0; i < items.Count; ++i)
{
spriteBatch.DrawString(font, items, position, Color.White);
}
}


This should do the trick. Although, I must warn you, there is no quarantee that the items and positions are in the same order. I would suggest creating a new class for each menu object, which would contain the string and the position for the object. Then, you could have a list of these new menu objects and draw them instead. smile.png

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement