0

我正在尝试使用 XNA 编写 Windows 游戏,但到目前为止我一直在绘制菜单。这是我的代码:

游戏.cs:

  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);

    }

菜单.cs:

public void Update()
    {
        for (int i = 0; i < 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 < buttons.Length; i++){
            buttons[i].Clickable = true;
        }
        spriteBatch.Begin();
        for (int i = 0; i < 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;

}

按钮.cs:

 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 && 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();

    }
}

在对 Menu 类进行编码之前,我只是使用Draw()每个按钮并且工作正常。然后,出了点问题,我不知道为什么,但我无法显示optionsMenu。我究竟做错了什么。有人能说出这段代码有什么问题以及为什么我不能显示菜单吗?

4

0 回答 0