{"id":18,"date":"2008-03-16T22:56:00","date_gmt":"2008-03-17T03:56:00","guid":{"rendered":"http:\/\/lukerymarz.com\/alearningexperience\/2008\/03\/basic-menu.html"},"modified":"2008-03-16T22:56:00","modified_gmt":"2008-03-17T03:56:00","slug":"basic-menu","status":"publish","type":"post","link":"http:\/\/lukerymarz.com\/alearningexperience\/2008\/03\/basic-menu.html","title":{"rendered":"Basic Menu"},"content":{"rendered":"<p>Well, it&#8217;s been busy lately, but I&#8217;ve got a solid start on the menu, so I thought I&#8217;d post what I have as a forcing function to make me clean up my code.  You can grab the source <a href=\"http:\/\/www.lukerymarz.com\/blog\/GameMenuSample.zip\">here<\/a>.  To use the menu system, you need to an instance of the GameMenu in you code.  You can declare this in you game class (don&#8217;t forget to put &#8220;<!-- {\\rtf1\\ansi\\ansicpg\\lang1024\\noproof1252\\uc1 \\deff0{\\fonttbl{\\f0\\fnil\\fcharset0\\fprq1 Courier New;}}{\\colortbl;??\\red0\\green0\\blue255;\\red255\\green255\\blue255;\\red0\\green0\\blue0;}??\\fs20 \\cf1 using\\cf0  GameMenu;\\par ??} --><span style=\"color: blue;\">using<\/span> GameMenu;&#8221; at the top of your code&#8221;):<\/p>\n<p><!-- {\\rtf1\\ansi\\ansicpg\\lang1024\\noproof1252\\uc1 \\deff0{\\fonttbl{\\f0\\fnil\\fcharset0\\fprq1 Courier New;}}{\\colortbl;??\\red43\\green145\\blue175;\\red255\\green255\\blue255;\\red0\\green0\\blue0;}??\\fs20 \\cf1 Menu\\cf0  mainMenu;} --> <\/p>\n<div style=\"background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;\">\n<p style=\"margin: 0px;\"><span style=\"color: rgb(43, 145, 175);\">Menu<\/span> mainMenu;<\/p>\n<\/div>\n<p>Then, in LoadContent(), initialize the menu, it&#8217;s choices, and any handlers you want to catch.<\/p>\n<p><!-- {\\rtf1\\ansi\\ansicpg\\lang1024\\noproof1252\\uc1 \\deff0{\\fonttbl{\\f0\\fnil\\fcharset0\\fprq1 Courier New;}}{\\colortbl;??\\red0\\green0\\blue0;\\red255\\green255\\blue255;\\red0\\green0\\blue255;\\red43\\green145\\blue175;\\red163\\green21\\blue21;}??\\fs20 mainMenu = \\cf3 new\\cf0  GameMenu.\\cf4 Menu\\cf0 (\\cf3 this\\cf0 );\\par ??\\par ??            mainMenu.AddChoice(\\cf5 \"Show Sprite\"\\cf0 );\\par ??            mainMenu.AddChoice(\\cf5 \"Return\"\\cf0 );\\par ??\\par ??            mainMenu.ChoiceExecuted += \\cf3 new\\cf0  \\cf4 Menu\\cf0 .\\cf4 ChoiceExecutedHandler\\cf0 (ChoiceExecuted);} --> <\/p>\n<div style=\"background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;\">\n<p style=\"margin: 0px;\">mainMenu = <span style=\"color: blue;\">new<\/span> GameMenu.<span style=\"color: rgb(43, 145, 175);\">Menu<\/span>(<span style=\"color: blue;\">this<\/span>);<\/p>\n<p style=\"margin: 0px;\">\n<p style=\"margin: 0px;\">mainMenu.AddChoice(<span style=\"color: rgb(163, 21, 21);\">&#8220;Show Sprite&#8221;<\/span>);<\/p>\n<p style=\"margin: 0px;\">mainMenu.AddChoice(<span style=\"color: rgb(163, 21, 21);\">&#8220;Return&#8221;<\/span>);<\/p>\n<p style=\"margin: 0px;\">\n<p style=\"margin: 0px;\">mainMenu.ChoiceExecuted += <span style=\"color: blue;\">new<\/span> <span style=\"color: rgb(43, 145, 175);\">Menu<\/span>.<span style=\"color: rgb(43, 145, 175);\">ChoiceExecutedHandler<\/span>(ChoiceExecuted);<\/p>\n<p><\/p>\n<p style=\"margin: 0px;\"><\/p>\n<p style=\"margin: 0px;\">Of course, ChoiceExecuted should be defined somewhere.  I&#8217;ll get to that at the end.  In your Update() method, call the game menu&#8217;s update function:<\/p>\n<p><\/p>\n<p style=\"margin: 0px;\"><\/p>\n<p style=\"margin: 0px;\"> <!-- {\\rtf1\\ansi\\ansicpg\\lang1024\\noproof1252\\uc1 \\deff0{\\fonttbl{\\f0\\fnil\\fcharset0\\fprq1 Courier New;}}{\\colortbl;??\\red0\\green0\\blue0;\\red255\\green255\\blue255;}??\\fs20 mainMenu.Update(gameTime);} --> <\/p>\n<div style=\"background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;\">\n<p style=\"margin: 0px;\">mainMenu.Update(gameTime);<\/p>\n<\/div>\n<p>And in the Draw() method, call the menu&#8217;s draw function:<\/p>\n<p><!-- {\\rtf1\\ansi\\ansicpg\\lang1024\\noproof1252\\uc1 \\deff0{\\fonttbl{\\f0\\fnil\\fcharset0\\fprq1 Courier New;}}{\\colortbl;??\\red0\\green0\\blue0;\\red255\\green255\\blue255;}??\\fs20 mainMenu.Draw(gameTime);} --> <\/p>\n<div style=\"background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;\">\n<p style=\"margin: 0px;\">mainMenu.Draw(gameTime);<\/p>\n<\/div>\n<p>If the menu isn&#8217;t visible, those functions will do nothing.  To show the menu, you set the visible property to true.  I do this in the Update method when the user pressed escape:<\/p>\n<p><!-- {\\rtf1\\ansi\\ansicpg\\lang1024\\noproof1252\\uc1 \\deff0{\\fonttbl{\\f0\\fnil\\fcharset0\\fprq1 Courier New;}}{\\colortbl;??\\red0\\green0\\blue255;\\red255\\green255\\blue255;\\red0\\green0\\blue0;\\red43\\green145\\blue175;}??\\fs20 \\cf1 if\\cf0  (keyboardState.IsKeyDown(\\cf4 Keys\\cf0 .Escape) &amp;&amp; !prevKeyBoardState.IsKeyDown(\\cf4 Keys\\cf0 .Escape))\\par ??            \\{\\par ??                mainMenu.visible = !mainMenu.visible;\\par ??            \\}} --> <\/p>\n<div style=\"background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;\">\n<p style=\"margin: 0px;\"><span style=\"color: blue;\">if<\/span> (keyboardState.IsKeyDown(<span style=\"color: rgb(43, 145, 175);\">Keys<\/span>.Escape) &amp;&amp; !prevKeyBoardState.IsKeyDown(<span style=\"color: rgb(43, 145, 175);\">Keys<\/span>.Escape))<\/p>\n<p style=\"margin: 0px;\">{<\/p>\n<p style=\"margin: 0px;\">   mainMenu.visible = !mainMenu.visible;<\/p>\n<p style=\"margin: 0px;\">}<\/p>\n<p>And finally, when a menu choice is executed, you handle it as follows:<\/p>\n<p><!-- {\\rtf1\\ansi\\ansicpg\\lang1024\\noproof1252\\uc1 \\deff0{\\fonttbl{\\f0\\fnil\\fcharset0\\fprq1 Courier New;}}{\\colortbl;??\\red0\\green0\\blue255;\\red255\\green255\\blue255;\\red0\\green0\\blue0;\\red43\\green145\\blue175;\\red163\\green21\\blue21;}??\\fs20 \\cf1 public\\cf0  \\cf1 void\\cf0  ChoiceExecuted(\\cf1 object\\cf0  source, \\cf4 Menu\\cf0 .\\cf4 MenuEvent\\cf0  e)\\par ??        \\{\\par ??            \\cf1 if\\cf0  (e.choiceString == \\cf5 \"Show Sprite\"\\cf0 )\\par ??            \\{\\par ??                mainMenu.visible = \\cf1 false\\cf0 ;\\par ??            \\}\\par ??            \\cf1 else\\cf0  \\cf1 if\\cf0  (e.choiceString == \\cf5 \"Return\"\\cf0 )\\par ??            \\{\\par ??                mainMenu.visible = \\cf1 false\\cf0 ;\\par ??            \\}\\par ??        \\}} --> <\/p>\n<div style=\"background: white none repeat scroll 0% 50%; font-family: Courier New; font-size: 10pt; color: black; -moz-background-clip: -moz-initial; -moz-background-origin: -moz-initial; -moz-background-inline-policy: -moz-initial;\">\n<p style=\"margin: 0px;\"><span style=\"color: blue;\">public<\/span> <span style=\"color: blue;\">void<\/span> ChoiceExecuted(<span style=\"color: blue;\">object<\/span> source, <span style=\"color: rgb(43, 145, 175);\">Menu<\/span>.<span style=\"color: rgb(43, 145, 175);\">MenuEvent<\/span> e)<\/p>\n<p style=\"margin: 0px;\">{<\/p>\n<p style=\"margin: 0px;\">   <span style=\"color: blue;\">if<\/span> (e.choiceString == <span style=\"color: rgb(163, 21, 21);\">&#8220;Show Sprite&#8221;<\/span>)<\/p>\n<p style=\"margin: 0px;\">   {<\/p>\n<p style=\"margin: 0px;\">       mainMenu.visible = <span style=\"color: blue;\">false<\/span>;<\/p>\n<p style=\"margin: 0px;\">   }<\/p>\n<p style=\"margin: 0px;\">   <span style=\"color: blue;\">else<\/span> <span style=\"color: blue;\">if<\/span> (e.choiceString == <span style=\"color: rgb(163, 21, 21);\">&#8220;Return&#8221;<\/span>)<\/p>\n<p style=\"margin: 0px;\">   {<\/p>\n<p style=\"margin: 0px;\">       mainMenu.visible = <span style=\"color: blue;\">false<\/span>;<\/p>\n<p style=\"margin: 0px;\">   }<\/p>\n<p style=\"margin: 0px;\">}<\/p>\n<p><\/p>\n<p style=\"margin: 0px;\">All I&#8217;m doing in the sample code is hiding the menu, but you can do anything in there.  There are also choiceSelected and choiceDeselected events, and they get fired in the current code.<\/p>\n<p><\/p>\n<p style=\"margin: 0px;\">My next update to this code should have the ability to navigate through menu pages, and the ability to give the user some left\/right choices as far as options go (a &#8220;Sound On Off&#8221; menu choice, for example).<\/p>\n<\/div>\n<p><\/p>\n<p style=\"margin: 0px;\">\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>Well, it&#8217;s been busy lately, but I&#8217;ve got a solid start on the menu, so I thought I&#8217;d post what I have as a forcing function to make me clean up my code. You can grab the source here. To use the menu system, you need to an instance of the GameMenu in you code. [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-18","post","type-post","status-publish","format-standard","hentry","category-xna"],"_links":{"self":[{"href":"http:\/\/lukerymarz.com\/alearningexperience\/wp-json\/wp\/v2\/posts\/18","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/lukerymarz.com\/alearningexperience\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/lukerymarz.com\/alearningexperience\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/lukerymarz.com\/alearningexperience\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/lukerymarz.com\/alearningexperience\/wp-json\/wp\/v2\/comments?post=18"}],"version-history":[{"count":0,"href":"http:\/\/lukerymarz.com\/alearningexperience\/wp-json\/wp\/v2\/posts\/18\/revisions"}],"wp:attachment":[{"href":"http:\/\/lukerymarz.com\/alearningexperience\/wp-json\/wp\/v2\/media?parent=18"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/lukerymarz.com\/alearningexperience\/wp-json\/wp\/v2\/categories?post=18"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/lukerymarz.com\/alearningexperience\/wp-json\/wp\/v2\/tags?post=18"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}