3

DrawString()我对in有问题XNA。我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>
4

3 回答 3

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

}

然后你有一个标签类:

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

赶上我的漂移?

于 2013-02-26T08:57:44.190 回答
2

看起来您的字体只允许您使用某些字符。

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

确保您的字符在开始和结束标记的 ASCII 值内,或者更改开始和结束标记以包含您想要的字符。

http://www.asciitable.com/

于 2013-02-26T04:45:39.580 回答
0

您使用的字体可能有问题。确保您使用的字体是“.ttf”并尝试几种不同的字体。我强烈建议您使用 Microsoft 推荐的字体之一。链接: http: //go.microsoft.com/fwlink/ ?LinkId=104778&clcid=0x409

于 2013-02-26T01:01:54.337 回答