0

モノゲーム ウィンドウでアプリケーションを実行しようとしています。画面に表示する長いテキストがあります。spriteBatch.Drawstring を使用して画面にレンダリングしようとしましたが、ある程度成功しました。しかし、テキストが必要な領域に収まりませんでした。私はこのチュートリアルに従っていました。テキスト全体を目的の領域内に配置するには、垂直スクロールを実装する必要があります。誰でも助けを提案できますか。これは私の現在のコードです:

protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);
        _boxTexture = new SolidColorTexture(GraphicsDevice, Color.Red);
        _borderRectangle = new Rectangle(100, 100, 500, 500);
        _textboxRectangle = new Rectangle(105, 105, 490, 490);
        _font = Content.Load<SpriteFont>("Rockwell");
        _text = "He determined to drop his litigation with the monastry, and relinguish his claims to the wood-cuting and fishery rihgts at once. He was the more ready to do this becuase the rights had becom much less valuable, and he had indeed the vaguest idea where the wood and river in quedtion were.";

    }
private String parseText(String text)
{
String line = String.Empty;
String returnString = String.Empty;
String[] wordArray = text.Split(' ');

foreach (String word in wordArray)
{
    if (font.MeasureString(line + word).Length() > textBox.Width)
    {
        returnString = returnString + line + '\n';
        line = String.Empty;
    }

    line = line + word + ' ';
}

return returnString + line;
}

および描画関数内:

spriteBatch.DrawString(font, parseText(text), new Vector2(textBox.X, textBox.Y), Color.White);
4

1 に答える 1

0

代わりに draw メソッドで行うことができます。次に、現在行っていることを実行しますが、返す文字列を作成する代わりに、呼び出すだけです

spriteBatch.DrawString(font, line, textPostion, Color.White);

代わりは。textPosition はテキストボックスの位置とちょうど同じですが、最初に、反復ごとに font.MeasureString(line).Y を使用して Y 位置を増やします。

textPosition.Y += font.MeasureString(line).Y; 

次に、チェックします

if(font.MeasureString(line).Y + textPosition.Y < textBox.Y + textBox.Height 
   || textPosition.Y > textBox.Y)    
{
     continue;
}

次に、たとえばキーボードの矢印の入力を探して (または上下にスクロールするためのボタンをいくつか作成して)、それに応じて textPosition.Y を増減します。次に、垂直にスクロールするテキストボックスが表示されます。

次に、位置の最小 Y 値を定義してロックを作成し、下または上にスクロールするとテキストが停止するようにすることができます。

于 2015-07-09T14:16:18.563 に答える