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]
2 replies to this topic
Sponsor:
#2 Members - Reputation: 135
Posted 08 March 2012 - 07:52 AM
The problem is this:
This draws each string to each of the positions, which isn't the way you want it. Try this:
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.
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[i], position[i], 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.






