Note that there are some explanatory texts on larger screens.

plurals
  1. POIssue with drawing graphical menus
    primarykey
    data
    text
    <p>I'm trying to code a Windows game using XNA and so far I'm stuck on drawing the menu. Here is my code:</p> <p>Game.cs:</p> <pre><code> protected override void LoadContent() { Text musicLevelText; musicLevelText.textValue = "Music :"; musicLevelText.location = new Vector2(400 - (int)font.MeasureString(musicLevelText.textValue).X - 20, 50); musicLevelText.font = font; Text soundEffectText; soundEffectText.textValue = "Sound Effects :"; soundEffectText.location = new Vector2(400 - (int)font.MeasureString(soundEffectText.textValue).X - 20, 10 + musicLevelText.location.Y + (int) font.MeasureString(musicLevelText.textValue).Y); soundEffectText.font = font; Text languageText; languageText.textValue = "Language :"; languageText.location = new Vector2(400 - (int)font.MeasureString(languageText.textValue).X - 20, 10 + soundEffectText.location.Y + (int)font.MeasureString(soundEffectText.textValue).Y); languageText.font = font; Button backToMainMenuButton = new Button(font, spriteBatch, "Back", new Vector2(20, 440)); optionsMenu = new Menu(new Button[1] {backToMainMenuButton}, new Text [3]{musicLevelText, soundEffectText, languageText}, background, spriteBatch); } </code></pre> <p>Menu.cs :</p> <pre><code>public void Update() { for (int i = 0; i &lt; buttons.Length; i++) { buttons[i].Update(); } } public void Draw() { spriteBatch.Begin(); spriteBatch.Draw(background, new Rectangle(0, 0, 800, 480), Color.White); spriteBatch.End(); if (!menuOn) return; for ( int i = 0; i &lt; buttons.Length; i++){ buttons[i].Clickable = true; } spriteBatch.Begin(); for (int i = 0; i &lt; texts.Length; i++) { spriteBatch.DrawString(texts[i].font, texts[i].textValue, texts[i].location, Color.White); } spriteBatch.End(); } public struct Text { public string textValue; public Vector2 location; public SpriteFont font; } </code></pre> <p>Button.cs :</p> <pre><code> public void Draw() { if (clickable) DrawButton(textRectangle); } private void DrawButton(Rectangle boundaries) { Vector2 size = font.MeasureString(text); float xScale = (boundaries.Width / size.X); float yScale = (boundaries.Height / size.Y); // Taking the smaller scaling value will result in the text always fitting in the boundaires. float scale = Math.Min(xScale, yScale); // Figure out the location to absolutely-center it in the boundaries rectangle. int strWidth = (int)Math.Round(size.X * scale); int strHeight = (int)Math.Round(size.Y * scale); Vector2 position = new Vector2(); position.X = (((boundaries.Width - strWidth) / 2) + boundaries.X); position.Y = (((boundaries.Height - strHeight) / 2) + boundaries.Y); // A bunch of settings where we just want to use reasonable defaults. float rotation = 0.0f; Vector2 spriteOrigin = new Vector2(0, 0); float spriteLayer = 0.0f; // all the way in the front SpriteEffects spriteEffects = new SpriteEffects(); // Draw the string to the sprite batch! Color color; if (clicked &amp;&amp; mouse.LeftButton == ButtonState.Pressed) { color = Color.Silver; } else { color = Color.White; } spriteBatch.Begin(); spriteBatch.DrawString(font, text, position, color, rotation, spriteOrigin, scale, spriteEffects, spriteLayer); spriteBatch.End(); } } </code></pre> <p>Before coding the Menu class, I was just using <code>Draw()</code> for each button and was working fine. Then, something went wrong and I don't know why but I can't display <code>optionsMenu</code>. What am I doing wrong. Can someone tell what's wrong with this code and why I can't display the menu?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload