XNA的DrawString()方法只绘制部分字符串。

3
我有一个与XNA中的DrawString()有关的问题。我为几个逻辑层使用了多个SpriteBatches,例如:背景、对象、菜单等等。
在我的菜单批处理器中,我绘制一个菜单(背景中的大灰色框),按钮(菜单上的较小灰色框)和按钮的字符串。
问题是: http://ompldr.org/vaGw4YQ/Unbenannt.png 但由于某种原因,字符串没有完全绘制。 有人知道原因吗?
编辑:
_menuLayer.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
        if (_menu != null)
        {
            _menuLayer.Draw(_menuBoard, new Vector2(graphics.PreferredBackBufferWidth / 2 - 160, graphics.PreferredBackBufferHeight / 2 - 240), Color.White);
        }
        _menuLayer.End();
        _buttonLayer.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
        if (_menu != null)
        {
            foreach (Button button in _menu.Buttons)
            {
                if (button.Pressed)
                {
                    _buttonLayer.Draw(_menuButtonPressed, button.Location, Color.White);
                    _buttonLayer.DrawString(_text, button.Text, button.GiveStringLocation(_text), Color.Black);
                }
                else
                {
                    _buttonLayer.Draw(_menuButton, button.Location, Color.White);
                    _buttonLayer.DrawString(_text, button.Text, button.GiveStringLocation(_text), Color.Black);
                }
            }
        }
        _buttonLayer.End();

<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains an xml description of a font, and will be read by the XNA
Framework Content Pipeline. Follow the comments to customize the appearance
of the font in your game, and to change the characters which are available to draw
with.
-->
<XnaContent xmlns:Graphics="Microsoft.Xna.Framework.Content.Pipeline.Graphics">
  <Asset Type="Graphics:FontDescription">

    <!--
    Modify this string to change the font that will be imported.
    -->
    <FontName>Arial</FontName>

    <!--
    Size is a float value, measured in points. Modify this value to change
    the size of the font.
    -->
    <Size>20</Size>

    <!--
    Spacing is a float value, measured in pixels. Modify this value to change
    the amount of spacing in between characters.
    -->
    <Spacing>0</Spacing>

    <!--
    UseKerning controls the layout of the font. If this value is true, kerning information
    will be used when placing characters.
    -->
    <UseKerning>true</UseKerning>

    <!--
    Style controls the style of the font. Valid entries are "Regular", "Bold", "Italic",
    and "Bold, Italic", and are case sensitive.
    -->
    <Style>Bold</Style>

    <!--
    If you uncomment this line, the default character will be substituted if you draw
    or measure text that contains characters which were not included in the font.
    -->
    <!-- <DefaultCharacter>*</DefaultCharacter> -->

    <!--
    CharacterRegions control what letters are available in the font. Every
    character from Start to End will be built and made available for drawing. The
    default range is from 32, (ASCII space), to 126, ('~'), covering the basic Latin
    character set. The characters are ordered according to the Unicode standard.
    See the documentation for more information.
    -->
    <CharacterRegions>
      <CharacterRegion>
        <Start>&#32;</Start>
        <End>&#126;</End>
      </CharacterRegion>
    </CharacterRegions>
  </Asset>
</XnaContent>

渲染菜单的代码是什么? - CodeCaster
剪切或遮挡是最有可能的原因,可能两者都有。除非你在做一些奇怪的事情,否则隐形墨水(笔颜色=背景颜色)不太可能出现。但从这里无法确定具体情况。 - Tony Hopkinson
嗨,看起来你正在使用Unicode字符(例如éÁ)。 默认情况下,Spritefont中不包括这些字符。 你能否发布你的Spritefont? - Steffan Donal
我已经添加了代码。希望这能让你更好地理解我的问题... :) - Marco Frost
谢谢。但是当我将它设置为不透明时,字母应该在的地方出现了黑色的东西... - Marco Frost
显示剩余4条评论
3个回答

2

看起来你的字体只能使用某些字符。

<CharacterRegions>
  <CharacterRegion>
    <Start>&#32;</Start>
    <End>&#126;</End>
  </CharacterRegion>
</CharacterRegions>

请确保您的字符要么在开始和结束标记的ASCII值范围内,要么将开始和结束标记更改为包含您想要的字符。

http://www.asciitable.com/


抱歉,但我使用的字母来自于普通拉丁字母表。它们位于两个极值之间。 - Marco Frost

2
尝试使用SpriteSortMode.Immediate。否则,问题可能出在你的操作方式上。将渲染留给UI之外的类似乱摆一样。你应该能够告诉一个按钮去绘制,然后它会自己绘制。或者只是告诉你的UI去绘制,然后它就会绘制所有的控件,包括他们的子控件等。

这样你就可以按层级构建你的UI:)

稍微结构化一下你的UI;有一个像UIControl的基类之类的东西;

List<UIControl> Children { get; protected set; }


Update(MyInputSystem input, float dt)
{
    UpdateChildren(input, dt);
}

UpdateChildren(MyInputSystem input, float dt)
{
    foreach(var child in Children)
        child.Update(input, dt);
}

Draw(SpriteBatch spriteBatch)
{
    DrawChildren(spriteBatch);
}

DrawChildren(SpriteBatch spriteBatch)
{
    foreach(var child in Children)
        child.Draw(spriteBatch);
}

那么您就有一个面板类:

class Panel: UIControl {

Vector2 Position { get; set; }
Texture2D Texture { get; set; }

override Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Draw(Texture, Position, Color.White);
    base.Draw(spriteBatch);
}

}

然后你有一个Label类:

class Label: Panel{

SpriteFont Font { get; set; }
String Text { get; set; }
Vector2 TextOffset { get; set; }
Color TextColor { get; set; }

override Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Draw(Texture, Position, Color.White);
    spriteBatch.DrawString(Font, Text, Position + TextOffset,Text Color);
    DrawChildren(spriteBatch);
}

}

同时还需要一个按钮类:

class Button: Label { //Inherit label, so we dont need to create som much new code

public event EventHandler<EventArgs> Click;

Rectangle _Bounds
public Rectangle Bounds
{
    get
    {
        if (_Rectangle.Width == 0)
            _Rectangle = new Rectangle((int)Position.X,(int)Position.Y, Texture.Width, Texture.Height);

        return _Texture;
    }
}

override Update(MyInputSystem input, flaot dt)
{
    if (input.MouseClicked && Bounds.Contains(input.MousePosition))
        Click(this, new EventArgs());
}

}

你可能已经注意到我这里没有任何SpriteBatch.Begin/End吗?那是因为:
class UI
{
List<UIControl> Controls;

Update(MyInputSystem input, flioat dt)
{
    foreach(var control in Controls)
       control.Update(input, dt);
}

Draw(SpriteBatch spriteBatch)
{
    spriteBatch.Begin(/*Parameters*/);
    foreach(var control in Controls)
       control.Draw(spriteBatch);
    spriteBatch.End();
}
}

你有跟上我的思路吗?

看起来很不错!我回家后会试一下。 :) - Marco Frost
哇!SpriteSortMode.Immediate 完全正常工作了!但是我想如果你同意的话,我也会尝试一下与你的 UI 结构一起工作。 :) - Marco Frost
没问题,如果你需要一些灵感;http://www.youtube.com/watch?v=yCuo2KFv-bE&list=UU0Sruv_Ad1bf3ElIdaoXqeQ&index=2 - Stig-Rune Skansgård

0

这看起来很有趣但很奇怪... 我会测试一下。 - Marco Frost

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接