Im trying to make a start screen and a menu for my game. But i got this problem which i dont know how to solve..
Every time i start my project it throws an exception. Here's the whole code
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
namespace SpaceCommander.Components
{
public class GameScreen : Microsoft.Xna.Framework.DrawableGameComponent
{
List<GameComponent> childComponents;
public GameScreen(Game game)
: base(game)
{
childComponents = new List<GameComponent>();
Visible = true;
Enabled = true;
}
public List<GameComponent> Components
{
get {return childComponents;}
}
public override void Initialize()
{
base.Initialize();
}
public override void Update(GameTime gameTime)
{
foreach ( GameComponent child in childComponents)
{
if (child.Enabled)
{
child.Update(gameTime);
}
}
base.Update(gameTime);
}
public virtual void Show()
{
Visible = true;
Enabled = true;
}
public virtual void Hide()
{
Visible = false;
Enabled = false;
}
public override void Draw(GameTime gameTime)
{
foreach (DrawableGameComponent child in childComponents)
{
child.Draw(gameTime);
}
base.Draw(gameTime);
}
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace SpaceCommander.Components
{
public class StartScreen: GameScreen
{
Menu menu;
public StartScreen(Game game, Texture2D texture, SpriteFont spriteFont)
: base(game)
{
string[] items = { "Single Player","Options", "Quit" };
menu = new Menu(game, spriteFont);
menu.SetMenuItems(items);
Components.Add(menu);
}
public int SelectedIndex
{
get { return menu.SelectedIndex; }
}
public override void Show()
{
menu.position = new Vector2(
(800 - menu.width) / 2, 330);
base.Show();
}
public override void Hide()
{
base.Hide();
}
}
}
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Collections.Specialized;
namespace SpaceCommander.Components
{
public class Menu : Microsoft.Xna.Framework.DrawableGameComponent
{
SpriteBatch spriteBatch = null;
SpriteFont spriteFont;
Color normal = Color.Yellow;
Color helistate = Color.Red;
public Vector2 position = new Vector2();
int selectedIndex = 0;
public StringCollection menuItems = new StringCollection();
public int width, heigh;
KeyboardState oldState;
public Menu(Game game, SpriteFont spriteFont)
: base(game)
{
this.spriteFont = spriteFont;
spriteBatch =
(SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
}
public int SelectedIndex
{
get { return selectedIndex; }
set
{
selectedIndex = (int)MathHelper.Clamp(
value,
0,
menuItems.Count - 1);
}
}
public void SetMenuItems(string[] items)
{
menuItems.Clear();
menuItems.AddRange(items);
CalculateBounds();
}
private void CalculateBounds()
{
width = 0;
heigh = 0;
foreach (string item in menuItems)
{
Vector2 size = spriteFont.MeasureString(item);
if (size.X > width)
width = (int)size.X;
heigh += spriteFont.LineSpacing;
}
}
public bool CheckKey(Keys theKey)
{
KeyboardState newState = Keyboard.GetState();
return oldState.IsKeyDown(theKey) && newState.IsKeyUp(theKey);
}
public override void Initialize()
{
base.Initialize();
}
public override void Update(GameTime gameTime)
{
KeyboardState newState = Keyboard.GetState();
if (CheckKey(Keys.Down))
{
selectedIndex++;
if (selectedIndex == menuItems.Count)
selectedIndex = 0;
}
if (CheckKey(Keys.Up))
{
selectedIndex--;
if (selectedIndex == -1)
{
selectedIndex = menuItems.Count - 1;
}
}
oldState = newState;
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
Vector2 menuPosition = position;
Color myColor;
for (int i = 0; i < menuItems.Count; i++)
{
if (i == SelectedIndex)
myColor = helistate;
else
myColor = normal;
spriteBatch.DrawString(
spriteFont,
menuItems[i],
menuPosition + Vector2.One,
Color.Black);
spriteBatch.DrawString(spriteFont,
menuItems[i],
menuPosition,
myColor);
menuPosition.Y += spriteFont.LineSpacing;
}
base.Draw(gameTime);
}
}
}
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;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Collections.Specialized;
namespace SpaceCommander.Components
{
public class Menu : Microsoft.Xna.Framework.DrawableGameComponent
{
SpriteBatch spriteBatch = null;
SpriteFont spriteFont;
Color normal = Color.Yellow;
Color helistate = Color.Red;
public Vector2 position = new Vector2();
int selectedIndex = 0;
public StringCollection menuItems = new StringCollection();
public int width, heigh;
KeyboardState oldState;
public Menu(Game game, SpriteFont spriteFont)
: base(game)
{
this.spriteFont = spriteFont;
spriteBatch =
(SpriteBatch)Game.Services.GetService(typeof(SpriteBatch));
}
public int SelectedIndex
{
get { return selectedIndex; }
set
{
selectedIndex = (int)MathHelper.Clamp(
value,
0,
menuItems.Count - 1);
}
}
public void SetMenuItems(string[] items)
{
menuItems.Clear();
menuItems.AddRange(items);
CalculateBounds();
}
private void CalculateBounds()
{
width = 0;
heigh = 0;
foreach (string item in menuItems)
{
Vector2 size = spriteFont.MeasureString(item);
if (size.X > width)
width = (int)size.X;
heigh += spriteFont.LineSpacing;
}
}
public bool CheckKey(Keys theKey)
{
KeyboardState newState = Keyboard.GetState();
return oldState.IsKeyDown(theKey) && newState.IsKeyUp(theKey);
}
public override void Initialize()
{
base.Initialize();
}
public override void Update(GameTime gameTime)
{
KeyboardState newState = Keyboard.GetState();
if (CheckKey(Keys.Down))
{
selectedIndex++;
if (selectedIndex == menuItems.Count)
selectedIndex = 0;
}
if (CheckKey(Keys.Up))
{
selectedIndex--;
if (selectedIndex == -1)
{
selectedIndex = menuItems.Count - 1;
}
}
oldState = newState;
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
Vector2 menuPosition = position;
Color myColor;
for (int i = 0; i < menuItems.Count; i++)
{
if (i == SelectedIndex)
myColor = helistate;
else
myColor = normal;
spriteBatch.DrawString(
spriteFont,
menuItems[i],
menuPosition + Vector2.One,
Color.Black);
spriteBatch.DrawString(spriteFont,
menuItems[i],
menuPosition,
myColor);
menuPosition.Y += spriteFont.LineSpacing;
}
base.Draw(gameTime);
}
}
}
If any1 could take a look at it, i tried everything. Also i think the problem is in draw method somewhere, because when i put Enabled to true and Visible to false, it doesnt throw an exception, only when i put visible to true.

New Topic/Question
Reply


MultiQuote




|