DrawString()
中的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> </Start>
<End>~</End>
</CharacterRegion>
</CharacterRegions>
</Asset>
</XnaContent>
答案 0 :(得分:2)
看起来您的字体只允许您使用某些字符。
<CharacterRegions>
<CharacterRegion>
<Start> </Start>
<End>~</End>
</CharacterRegion>
</CharacterRegions>
确保您的字符在开始和结束标记的ASCII值范围内,或更改开始和结束标记以包含您想要的字符。
答案 1 :(得分: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);
}
然后你有一个Panel-class:
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:
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();
}
}
抓住我的漂移?
答案 2 :(得分:0)
您使用的字体可能存在问题。确保您使用的字体是“.ttf”并尝试一些不同的字体。我强烈建议您使用Microsoft推荐的字体之一。链接:http://go.microsoft.com/fwlink/?LinkId=104778&clcid=0x409