how to create a play button and load game on play? Monogame

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

namespace giftman
{
{
List<GuiElement> gm = new List<GuiElement>();

{
GuiElement BackGround = new GuiElement("BackGround");
GuiElement PlayButton = new GuiElement("Play_Button");
GuiElement HighScore = new GuiElement("HighScore_Button");
GuiElement QuitButton = new GuiElement("Quit_Button");
}

{
foreach (var element in gm)
{
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()
{
graphics.PreferredBackBufferWidth = 800;
graphics.PreferredBackBufferHeight = 480;
IsMouseVisible = true;

if (IsPlayPressed == false)
{
//var guiElement = new GuiElement("Play_Button");

}

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 //
// Importent Systemcomponents End//

//Player's component //
player.GetTransform.GetPosition = new Vector2(50, 10);
//Player's component End//

//Player's_2 component //
player_2.GetTransform.GetPosition = new Vector2(200, 10);
//Player's_2 component End//

//Enemy Components //

enemy.GetTransform.GetPosition = new Vector2(80, 50);

// End Enemy Components //

}

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.

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.

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.

• 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 Feb 17 '16 at 21:52
• 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. Feb 17 '16 at 22:06
• 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 Feb 18 '16 at 8:59
• 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. Feb 18 '16 at 9:18
• 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. Feb 18 '16 at 9:59