I have a WindowsGameLibrary that I created using XNA (written in C#). It includes its own extension of the Game class. For whatever reason, the overridden version of LoadContent is never getting called. I’ve read in forums that this can be caused by accidentally overriding Initialize without adding base.Initialize() at the end, but I made sure to have that and it still doesn’t work. I will note that the namespace containing the extension of Game is separate from that of the actual WindowsGame project that has the “Program” class in it, though I don’t think that should matter. Below is my code:
public AttunedGame()
{
// Singleton
if (Instance == null) Instance = this;
this.currentArea = "";
}
protected override void Initialize()
{
// Managers
graphics = new GraphicsDeviceManager(this);
currentGameTime = new GameTime(new TimeSpan(0), new TimeSpan(0));
// Areas
areas = new CollectibleCollection<Area>();
foreach (string a in Directory.EnumerateDirectories(@"content\areas"))
{
areas.Add(new Area(a));
areas.Get(Collectible.IDFromPath(a)).Initialize();
}
base.Initialize();
}
protected override void LoadContent()
{
spriteBatch = new AnimatedSpriteBatch(this.GraphicsDevice);
}
// Main loop methods
protected override void Update(GameTime gameTime)
{
currentGameTime = gameTime;
CurrentArea.Update();
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
currentGameTime = gameTime;
SpriteBatch.Begin();
CurrentArea.Draw();
SpriteBatch.End();
base.Draw(gameTime);
}
I placed breakpoints in every method shown here to see where the program flow was going. It hit the constructor first, followed by Initialize (which I stepped through one line at a time until the end), and then went to Update and then Draw. I even let it loop for awhile and it never got to LoadContent.
Much thanks for any help.
You actually need to create the GraphicsDeviceManager inside the constructor. Move the line:
from inside
to being inside
My guess is that something inside XNA requires the graphics device manager to be initialized before it can load content (potentially the service container expects the graphics device to be made so that it can load content such as textures). LoadContent is actually called from the base class Initialize, which is probably failing somehow; it really should be throwing some sort of exception but they probably didn’t expect that case.
Hope that helps!