c# - all menu item's become selectable -
i trying create simple menu, string selected changes colur instructed do, can't seem space out each item in string list on y axis, positioned in middle @ top of screen overlapping each other. know simple fix novice @ xna. appreciated. in advance.
menumanagement class
using microsoft.xna.framework; using microsoft.xna.framework.content; using microsoft.xna.framework.graphics; using microsoft.xna.framework.input; using system; using system.collections.generic; using system.linq; using system.text; namespace game2 { class menumanagement { keyboardstate keyboard; keyboardstate prevkey; mousestate mouse; vector2 position; list<string> buttonlist = new list<string>(); spritefont spritefont; int selected = 0; public menumanagement () { buttonlist.add("play"); buttonlist.add("options"); buttonlist.add("instructions"); buttonlist.add("exit"); } private void measuremenu() { height = 0; width = 0; foreach (string item in buttonlist) { vector2 size = spritefont.measurestring(item); height += spritefont.linespacing + 5; } } public void loadcontent (contentmanager content ) { spritefont = content.load<spritefont>("spritefont"); } public void update (gametime thegametime) { keyboard = keyboard.getstate(); if (checkkeyboard(keys.up)) { if (selected > 0) { selected--; } } if (checkkeyboard(keys.down)) { if (selected < buttonlist.count - 1) { selected++; } } prevkey = keyboard; } public bool checkkeyboard (keys key) { return (keyboard.iskeydown(key) && prevkey.iskeydown(key)); } public void draw (spritebatch thespritebatch) { thespritebatch.begin(); color color; (int = 0; < buttonlist.count; i++) { vector2 location = position; if (i == selected) { color = color.yellow; } else { color = color.blue; } location.y += spritefont.linespacing + 5; } thespritebatch.drawstring(spritefont, buttonlist[0], new vector2 (300, 100), color); thespritebatch.drawstring(spritefont, buttonlist[1], new vector2(300, 150), color); thespritebatch.drawstring(spritefont, buttonlist[2], new vector2(300, 200), color); thespritebatch.drawstring(spritefont, buttonlist[3], new vector2(300, 250), color); thespritebatch.end(); } } }
game1
using microsoft.xna.framework; using microsoft.xna.framework.graphics; using microsoft.xna.framework.input; namespace game2 { /// <summary> /// main type game. /// </summary> public class game1 : game { graphicsdevicemanager graphics; spritebatch thespritebatch; public static rectangle screen; public static string gamestate = "menu"; menumanagement menumanagement; public game1() { graphics = new graphicsdevicemanager(this); content.rootdirectory = "content"; } protected override void initialize() { // todo: add initialization logic here menumanagement = new menumanagement(); screen = new rectangle(0, 0, graphics.preferredbackbufferwidth, graphics.preferredbackbufferheight); base.initialize(); } protected override void loadcontent() { thespritebatch = new spritebatch(graphicsdevice); menumanagement.loadcontent(content); // todo: use this.content load game content here } protected override void unloadcontent() { // todo: unload non contentmanager content here } // <param name="gametime">provides snapshot of timing values.</param> protected override void update(gametime gametime) { if (gamepad.getstate(playerindex.one).buttons.back == buttonstate.pressed || keyboard.getstate().iskeydown(keys.escape)) exit(); switch (gamestate) { case "menu": menumanagement.update(gametime); break; } base.update(gametime); } /// <param name="gametime">provides snapshot of timing values.</param> protected override void draw(gametime gametime) { switch (gamestate) { case "menu": menumanagement.draw(thespritebatch); break; } base.draw(gametime); } } }
you can calculate length of strings , place next item want. should spritefont.measurestring().
https://msdn.microsoft.com/en-us/library/bb464128(v=xnagamestudio.30).aspx
to clear, reason overlapping because putting them in same position:
vector2 location = position;
and then:
new vector2 (300, location.y)
is drawing entire batch. need change position of each of menu items. simplest fix increment y value of location each item. move next menu item toward bottom. increment x move right.
Comments
Post a Comment