2
\$\begingroup\$

So I got my game in another class and my main menu in another class.

This is the update method in my Main menu class

MouseState mouse = Mouse.GetState();
            switch (CurrentGameState)
            {
                case GameState.MainMenu:
                    if (btnPlay.isClicked == true) CurrentGameState = GameState.Playing;
                    btnPlay.Update(mouse);
                    break;

                case GameState.Playing:

                    break;

            }
            base.Update(gameTime);

This is the draw method in my Main menu class

spriteBatch.Begin();
            switch (CurrentGameState)
            {
                case GameState.MainMenu:
                    spriteBatch.Draw(Content.Load<Texture2D> ("MainMenu"), new Rectangle (0,0, screenWidth, screenHeight),Color.White);
                    btnPlay.Draw(spriteBatch);

                    break;

                case GameState.Playing:
                    break;
                                    }

            spriteBatch.End();

But now I don't know how to link/call the actual game file.. Would someone be able to help me with this please?

\$\endgroup\$
5
  • \$\begingroup\$ Typically you would pass the values you need from your Game class , to your other classes. What did you need to access the Game class for exactly? \$\endgroup\$
    – jgallant
    Commented Dec 7, 2015 at 15:14
  • \$\begingroup\$ I don't think I understood your question... What I did was I made the game and then I found a tutorial online and made a main menu.. But now when I click the play button it doesn't bring up my game so I was wondering how do I link those? \$\endgroup\$
    – hTeeML
    Commented Dec 7, 2015 at 15:17
  • \$\begingroup\$ You would instantiate the Main Menu object from your Game class, and then call its Update/Draw functions at the least. Have you attempted to instantiate the Main Menu from inside your game class yet? \$\endgroup\$
    – jgallant
    Commented Dec 7, 2015 at 15:19
  • \$\begingroup\$ No I don't think so.. I don't know how to be honest \$\endgroup\$
    – hTeeML
    Commented Dec 7, 2015 at 15:22
  • \$\begingroup\$ In my current game I'm create GameContext class and dumped to it all global staff, like asset, particle and input managers, random object, etc. I'm not shure is that a good idea in big project, but for me ok. Also, there is active UI index, and Game class draw appropriate UI based on it. \$\endgroup\$
    – user62425
    Commented Dec 7, 2015 at 15:40

1 Answer 1

1
\$\begingroup\$

As first, don't do it:

spriteBatch.draw(Content.Load<Texture2D>("blabla"), ...);

Because you are calling the "Load" method in every frame. That would cause a memory leak.

And if I understand your problem correctly, your

First option: Using Static Values

These static values should be in your Game class.

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    public static Texture2D globalTexture; //This is reachable from other classes

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }
    //...

public class MainMenu
{
    //...
    public void drawItem(SpriteBatch sb)
    {
        sb.draw(Game1.globalTexture, ...);
    }
}

Second option: Using an instance of the Game class

public class MainMenu
{
    Game1 gameInstance;

    public MainMenu(Game1 theGame)
    {
        gameInstance = theGame;
    }

    //...
    public void drawItem(SpriteBatch sb)
    {
        Texture2D texture = gameInstance.texture;

        sb.draw(texture, ...);
    }
}

Or you do using a parameter:

public class MainMenu
{
    //...
    public void drawItem(Game1 game)
    {
        SpriteBatch sb = game.spriteBatch;
        Texture2D texture = game.texture;

        sb.draw(texture, ...);
    }
}


You can also have the necessary things as parameter in the public methods, which get called by the game class, like that:

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;
    MainMenu menu = new MainMenu();
    private float rotation;

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    public void drawMenu()
    {
        menu.draw(spritebatch, rotation, ...);
    }
    //...

public class MainMenu
{
    //...
    public void draw(SpriteBatch sb, float rotation)
    {
        float playerRot = rotation;
        sb.draw(Game1.globalTexture, ... , playerRot);
    }
}
\$\endgroup\$
0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .