Inside of my main class file I am initializing a number of game components which are used to represent game scenes i.e. main menu and game. I am initilizing each component inside of XNAs LoadContent method without issue and set the default scene as the main menu. I then switch to the game scene.
My issue then occurs when I switch back to the main menu scene and then try to start a new game by switch from the menu to the game scene. When I switch from the game to the menu I dispose of the game scene using the GameComponent.Dispose() method provided by the XNA framework. As I have disposed of the game scene I attempt to re-initialize the game scene component with the following method.
private void LoadScene(GameScene scene)
{
scene = new GameScene(this, graphicsDevicegraphics.GraphicsDevice);
Components.Add(scene);
}
I then switch from the menu scene to the game scene using the following method. This method is called in my Update method after the user has selected new game on the main menu.
public void SwitchScene(SceneManager manager)
{
sceneManager.HideScene();
sceneManager = manager;
manager.ShowScene();
}
This time when the game scene loads I am met with a cornflower blue background and no assets on screen. I have placed a breakpoint within the above method and seen that the game scene is given all of the required data yet nothing is being drawn.
However if I explicitly place the following code above the SwitchScene() method after the player has selected new game then the scene is drawn without issue.
scene = new GameScene(this, graphics.GraphicsDevice);
Components.Add(scene);
Below is what the code looks like with the second approach mentioned.
if (InputHandler.IsHoldingButton(0, Buttons.A, out throwAway))
{
switch (menuScene.Index)
{
case 0:
gameScene = new GameScene(this, graphics.GraphicsDevice);
Components.Add(gameScene);
SwitchScene(gameScene);
break;
case 1:
this.Exit();
break;
}
}
Is this behaviour intentional, if so how would I attempt to produce the first approach correctly?
Below is a screenshot of the graphics device after the second attempt to load the game scene. Note the data in this image is identical to the graphics device when loading the game scene for the first time.
//edited grammatical errors