-1
\$\begingroup\$

I need help with understanding how I can make my function work and load the game when pressing play.

At the moment, when I press Play, it won't update or missing update o knowing the button is pressed

Entier menu seperate class:

namespace giftman
{
    class GameMenu : Component,ILoadable,IUpdateable,IDrawable
    {
        List<GuiElement> gm = new List<GuiElement>();

        public GameMenu()
        {
            GuiElement BackGround = new GuiElement("BackGround");                      
            gm.Add(BackGround);
            GuiElement PlayButton = new GuiElement("Play_Button");                       
            gm.Add(PlayButton);          
            GuiElement HighScore = new GuiElement("HighScore_Button");          
            gm.Add(HighScore);
            GuiElement QuitButton = new GuiElement("Quit_Button");
            gm.Add(QuitButton);
        }

        public void LoadContent(ContentManager content)
        {                                        
            foreach (var element in gm)
            {
                element.LoadContent(content);
                element.CenterElement(480,800);
                element.clickEvent += OnClick;
            }
            gm.Find(X => X.AssetName == "Play_Button").MoveElement(0,-120);
            gm.Find(X => X.AssetName == "HighScore_Button").MoveElement(0, -37);
            gm.Find(X => X.AssetName == "Quit_Button").MoveElement(0, 50);
        }

        public void Update()
        {
            foreach (GuiElement element in gm)
            {
                element.Update();
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            foreach (GuiElement element in gm)
            {
                element.Draw(spriteBatch);
            }
        }

        public void OnClick(string element)
        {
            if (element == "Play_Button")
                GameWorld.Instance.isplaypresssed = true;

            if (element == "HighScore_Button")
            {
                // Play the Game //
            }

            if (element == "Quit_Button")
                // Quit The game //
               GameWorld.Instance.Exit();
        }
    }
}

...and the place I load it all another class, GameWorld:

protected override void Initialize()
{
    // TODO: Add your initialization logic here
    graphics.PreferredBackBufferWidth = 800;
    graphics.PreferredBackBufferHeight = 480;
    IsMouseVisible = true;

    if (IsPlayPressed == false)
    {
        var gamemenu = new GameObject();
        //var guiElement = new GuiElement("Play_Button");
        // GameMenu and functions load here //
        gamemenu.AddComponent(new GameMenu());
        gamemenu.Update();
        // End GameMenu //

        gameObjects.Add(gamemenu);
    }

    if (isplaypresssed)
    {
        var map = new GameObject();
        var player = new GameObject();
        var player_2 = new GameObject();
        var enemy = new GameObject();
        var fruit = new GameObject();
        var gamecomponents = new GameObject();

        // Static Map Component //
        map.AddComponent(new GameMap(map, "pac_man_map", new Rectangle(0, 0, this.GraphicsDevice.Viewport.Width, this.GraphicsDevice.Viewport.Height), 0));
        // Static Map Component End//

        // Importent Systemcomponents //
        gamecomponents.AddComponent(new ScoreSystem());
        gamecomponents.AddComponent(new LifeSystem());
        // Importent Systemcomponents End//

        //Player's component //
        player.AddComponent(new SpriteRenderer(player, "Player_1", 1));
        player.GetTransform.GetPosition = new Vector2(50, 10);
        player.AddComponent(new Animator(player));
        player.AddComponent(new Player(player));
        //Player's component End//

        //Player's_2 component //
        player_2.AddComponent(new SpriteRenderer(player_2, "Player_2", 1));
        player_2.GetTransform.GetPosition = new Vector2(200, 10);
        player_2.AddComponent(new Animator(player_2));
        player_2.AddComponent(new Player_2(player_2));
        //Player's_2 component End//

        //Enemy Components //

        enemy.AddComponent(new SpriteRenderer(enemy, "GiftMan_animated3.0", 1));
        enemy.GetTransform.GetPosition = new Vector2(80, 50);

        enemy.AddComponent(new Animator(enemy));
        enemy.AddComponent(new Enemy(enemy));

        // End Enemy Components //

        fruit.AddComponent(new Fruit(fruit, "cherry", 10));

        gameObjects.Add(map);
        gameObjects.Add(gamecomponents);
        gameObjects.Add(fruit);
        gameObjects.Add(player);
        gameObjects.Add(player_2);
        gameObjects.Add(enemy);
    }

    base.Initialize();
}

What I need help with is understanding how I can make it change the IsPlayPressed to true and then switch to the game.

\$\endgroup\$
0
\$\begingroup\$

You've got two different checks in here, at

    if (IsPlayPressed == false)

and at

    if (isplaypresssed)

Since one of these must be wrong, make sure to correct the capitalization of the wrong one.

\$\endgroup\$
0
\$\begingroup\$

In your gameworld class you have an initialize function. From your code it is unclear when that function is called (when do you instantiate the gameworld?).

Either way, from the looks of it the Initialize is generally only called upon creation. It appears that when it is instantiated the PressedButton boolean is set to false. By setting the boolean to true doesn't automatically call the function Initialize again. Thus your gameworld is never actually initialized.

You may want to look into gamestates. A gamestate mechanism is better than setting a boolean.

-edit: also ChrisUC's answer. You seem to have inconsistencies in your variable names.

\$\endgroup\$
9
  • \$\begingroup\$ ye i know. i just dont know how i can force a update again and there's no need for extra code for u guys to know i think since it's being called from gameworld and i prob need to update it somehow. if u have a idea on how that would be cool \$\endgroup\$ Feb 17 '16 at 21:52
  • \$\begingroup\$ To be honest, that seems programming 101 knowledge. If you can set a value in another object, you can also call a method in another object. Your other game objects need to interact together as well so you will need to understand how to make objects call functions in other classes. \$\endgroup\$
    – Felsir
    Feb 17 '16 at 22:06
  • \$\begingroup\$ Yes its 101 :D but still i know what i need to do but i somehow dont know how to get it out of my head if u know what i mean \$\endgroup\$ Feb 18 '16 at 8:59
  • \$\begingroup\$ You might want to start with a small test project- a small game to test a few things. Even if it is just a box changing color on a keypress. Practise makes perfect. Eventually you will get there! Also read about game structures and design patterns. Take a look here: gameprogrammingpatterns.com/contents.html (the book is also available as e-book or in print). A lot of the patterns and examples translate to any game framework including XNA/Monogame. \$\endgroup\$
    – Felsir
    Feb 18 '16 at 9:18
  • \$\begingroup\$ can't do mate gotta turn in project tommorw its a school project afterall and we only had 1 week of teaching Monogame so ye i am really in the basic levels we are using a component pattern but i am finding it hard to fiqure out how i should update the gamestate change. \$\endgroup\$ Feb 18 '16 at 9:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.