I’m solo working on a video game as a hobby and decided to give MonoGame a try. I’m a beginner so I’m not very knowledgeable. Everything works fine for now, except the game creating a totally useless window when starting the MainMenu scene and leaving the dead Intro window as a leftover. This is not alright, especially as my game will grow and when I will have dozens of scenes. I guess the issue is caused by the fact the Run() instruction creates a new instance for MainMenu when called. I tried a few code snippets, but they seem to simply don’t work for my code/specific situation. All I want is the game to keep a single window/instance when switching to different scenes. For now, my game have 3 files: scenes.cs, intro.cs and mainmenu.cs. I will upload their structure here to help you understand better what I’m talking about. I would greatly appreciate some help, thank you!
SCENES.CS:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
namespace MyGame
{
public class Scene : Game
{
protected GraphicsDeviceManager _graphics;
protected SpriteBatch _spriteBatch;
public Scene()
{
_graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
IsMouseVisible = true;
_graphics.IsFullScreen = true;
_graphics.PreferredBackBufferWidth = 1366;
_graphics.PreferredBackBufferHeight = 768;
_graphics.PreferMultiSampling = false;
_graphics.ApplyChanges();
}
protected override void Initialize()
{
base.Initialize();
}
protected override void LoadContent()
{
_spriteBatch = new SpriteBatch(GraphicsDevice);
}
public void Switch(Scene scene)
{
Content.Unload();
Exit();
scene.Run();
}
}
}
INTRO.CS:
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace MyGame
{
public class Intro : Scene
{
// Constructor
public Intro()
{
}
protected override void Initialize()
{
// Initialization
}
protected override void LoadContent()
{
// Content load
}
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
Exit();
// Logo displaying logic
else if (_fadingComplete && _logoDisplayed)
{
Switch(new MainMenu()); // Here I call Switch to switch to the MainMenu scene
}
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
// Drawing logic
base.Draw(gameTime);
}
}
}
MAINMENU.CS:
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
namespace DreamOdyssey
{
public class MainMenu : Scene
{
// Constructor and Initialization
protected override void LoadContent()
{
// Content loading
}
protected override void Update(GameTime gameTime)
{
// Update logic
}
protected override void Draw(GameTime gameTime)
{
// Draw logic
}
}
}