Hy everyone, so I've being trying to understand how can I release my Main Menu from memory and screen and so on when
the game is started. So far I have done this, so in my Menu class I have a class called MenuButton and it looks like this:
public class MenuButton
{
public Texture2D buttonTxd;
public Rectangle buttonRect;
public int ButtonHeight;
public int ButtonWidth;
public bool isClicked()
{
if (Game1.GetCursorRect().Intersects(buttonRect) && Mouse.GetState().LeftButton == ButtonState.Pressed)
{
return true;
}
else return false;
}
public bool isHovered()
{
if (Game1.GetCursorRect().Intersects(buttonRect))
{
return true;
}
else return false;
}
}
This is inside Menu class and Menu class also dosen't contain a constructor becouse I am mainly doing all the work inside Menu class.
Now after MenuButton class I create 2 new instances of that Class, one is called Play and one is called Quit .
Then I have Initialize and Load Content functions, which yous initialize buttons and some Menu variables and well Load Content loads Textures soundEffect and a Song.
Now at the end of my Menu class I have these 3 functions:
// Menu Release
public void Dispose()
{
Play = null; // Button
Quit = null; // Button
hButtonSnd = null; // Sound played when button is hovered
GC.Collect();
}
public bool isDefined(MenuButton mb1) // isDefined 1 arg
{
if (mb1 == null)
{
return false;
}
else return true;
}
public bool isDefined(MenuButton mb1, MenuButton mb2) // isDefined 2 args
{
if (mb1 == null || mb2 == null)
{
return false;
}
else return true;
}
Between that and Load Content() are ofcourse my Update and Draw methods, now here is an Update one:
public void Update()
{
if((Play.isHovered() || Quit.isHovered()) && isDefined(Play, Quit)) // As you can see I also checked if
// Play and Quit weren't null
{
if (EnableHoverSound)
{
hButtonSnd.Play();
EnableHoverSound = false;
}
}
else EnableHoverSound = true;
if (Play.isClicked())
{
PlayGame = 1;
MediaPlayer.Stop();
Dispose(); // Sets buttons and a sound to NULL and GC.Collect()
}
}
Now ofcourse I have to show you some code from my Game1.cs file, at the beggining of Game1 I declare a new instance of Menu menu = new Menu(); ,called menu.Initialize() in Game1 Initialize method and also menu.LoadContent(Content), in LoadContent() method, and now this is how my Game1 Update() looks like:
protected override void Update(GameTime gameTime)
{
if (menu.PlayGame == 1) // this is the same as menu.Play.isClicked()
{
if (fade.A == 1) // related to cursor fading if Play is clicked
fade.A++;
else fade.A--;
}
elapsed_time += (float)gameTime.ElapsedGameTime.TotalMilliseconds;
if (elapsed_time >= 1000.0f)
{
fps = total_frames;
total_frames = 0;
elapsed_time = 0;
}
if (menu.Quit.isClicked() && menu.isDefined(menu.Quit) || Keyboard.GetState().IsKeyDown(Keys.Escape))
{
this.Exit();
MediaPlayer.Stop();
}
cursorRect = new Rectangle(Mouse.GetState().X, Mouse.GetState().Y, curText.Width, curText.Height);
if (menu.isDefined(menu.Play, menu.Quit))
{
menu.Update();
}
base.Update(gameTime);
}
And finally I have to show you just my Draw() method here:
protected override void Draw(GameTime gameTime)
{
total_frames++;
GraphicsDevice.Clear(fade);
spriteBatch.Begin();
if (menu.isDefined(menu.Play, menu.Quit))
{
menu.Draw(spriteBatch);
}
spriteBatch.Draw(curText, cursorRect, fade);
DrawHelp();
spriteBatch.End();
base.Draw(gameTime);
}
Now after showing you all this code here is what my game does:
1.I enter the game, I hear my Song playing, all good
2. I hover my Play and Quit buttons and they play a sound like they should
3. If I Click Quit button the game will quit
4. If I Click Play button The Song will stop, sounds also, no more Play and Quit buttons thats what I want, BUT THEN THE GAME FREEZES
I cant move moouse cursor, it wont minimize, I have to do Start + L to get to that chose user Screen to get back in and shut the thing with taskmgr.exe
This might be a long post but I wanted to post all relevant code so that some good guys could just read it quickly and say aha here is your mistake. Hahah, thanks in advanced :)