【问题标题】:XNA - Drawing Strings on MenusXNA - 在菜单上绘制字符串
【发布时间】:2013-05-09 08:43:22
【问题描述】:

关于在菜单中绘制 XNA 上的字符串列表的两个问题。首先是你如何左对齐文本而不是居中对齐呢?其次是你如何在选择任何一个之前显示一个说一行? 这是我到目前为止的代码;

    public void Draw(SpriteBatch spriteBatch, GameTime gameTime)
    {
        Color color;
        int linePadding = 3;

        if (gameTime.TotalGameTime.TotalSeconds <= 3)
        {
            spriteBatch.Draw(mTheQuantumBros2, new Rectangle(300, 150, mTheQuantumBros2.Width, mTheQuantumBros2.Height), Color.White);
        }
        else
        {
            for (int i = 0; i < buttonList.Count; i++)
            {
                color = (i == selected) ? Color.Gold : Color.LawnGreen;
                spriteBatch.DrawString(spriteFont, buttonList[i], new Vector2((screenWidth / 2) - (spriteFont.MeasureString(buttonList[i]).X / 2), (screenHeight / 2) - (spriteFont.LineSpacing * (buttonList.Count) / 2) + ((spriteFont.LineSpacing + linePadding) * i)), color);
            }
        }
    }

【问题讨论】:

    标签: c# menu xna


    【解决方案1】:

    嗯.. 老实说,我认为中心对齐会更好看,但在您的代码中,您需要做的就是将绘制字符串更改为:

    for (int i = 0; i < buttonList.Count; i++)
    {
         color = (i == selected) ? Color.LawnGreen : Color.Gold;
         spriteBatch.DrawString(spriteFont, buttonList[i], new Vector2((screenWidth / 2) /*- (spriteFont.MeasureString(buttonList[i]).X / 2)*/, (screenHeight / 2) - (spriteFont.LineSpacing * (buttonList.Count) / 2) + ((spriteFont.LineSpacing + linePadding) * i)), color);
    }
    

    您可以看到我注释掉的内容。通过去掉它,您不再将字符串向左移动其宽度的一半。

    至于第二部分,您需要做的就是检查是否选择了该值,因为您已经在确定菜单项颜色。如果选中,只需添加“|”到字符串的开头和结尾:

    for (int i = 0; i < buttonList.Count; i++)
    {
         color = (i == selected) ? Color.LawnGreen : Color.Gold;
         if(i != selected)
         {
             spriteBatch.DrawString(spriteFont, buttonList[i], new Vector2((screenWidth / 2) /*- (spriteFont.MeasureString(buttonList[i]).X / 2)*/, (screenHeight / 2) - (spriteFont.LineSpacing * (buttonList.Count) / 2) + ((spriteFont.LineSpacing + linePadding) * i)), color);
         }
         else
         {
             spriteBatch.DrawString(spriteFont,"|" + buttonList[i] + "|", new Vector2((screenWidth / 2) - (int)spriteFont.MeasureString("|").X, (screenHeight / 2) - (spriteFont.LineSpacing * (buttonList.Count) / 2) + ((spriteFont.LineSpacing + linePadding) * i)), color);
         }
    }
    

    现在,由于您要添加“|”到字符串的开头,这将影响位置。通过减去“|”的测量宽度,然后将字符串重新居中。

    【讨论】:

    • 首先,我希望它左对齐,因为我想如何移动它,因为您非常乐于助人,当我完全完成菜单时,我会向您展示并询问您的意见。其次,您将如何制作 |在屏幕前面是一个指定的颜色?第三,在第二个 spritebatch.DrawString 中,是左对齐还是中心对齐?
    • 试试看:p。如果您还想更改颜色,则必须在不同的字符串中单独绘制它。您只需调用 drawstring() 两次,每个“|”一次,将它们放置在相同的 x 值减去第一个字符串的字符串测量宽度 ('|'),然后添加按钮字符串的宽度到第二个字符串的 x 坐标。
    • 另外,通过左对齐文本,不可能使它们都与屏幕中心对齐。无论如何,不​​幸的是,它们在数学上都会关闭。如果您只是尝试将这些写在一张纸上,您就会看到这一点。只有一个字符串可以与中心对齐(所有相同长度的字符串也将对齐),但其他字符串必须与另一个字符串的前面对齐。
    • 如何找到所选字符串的 x 和 y 坐标?我也没有把它们放在中间,那只是在我制作按钮的时候
    • 使用与放置它们相同的数学。计算时只需存储所选值的 x 和 y 变量即可。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-16
    • 1970-01-01
    • 2013-07-13
    • 2021-11-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多