2011-12-31 5 views
0

今日はXNAを使いこなし始めましたが、まだC#を学んでいます。私はゲームのメインメニューを制作しようとしています。XNAのテキストにメソッドを実装する

私はスプライトフォントファイルを作成し、私が望むテキストを生成しています。このためのコードは次のとおりです。

spriteBatch.DrawString(font, ">://Start Game [1]", new Vector2(0, 0), Color.LimeGreen); 

私の質問は、私は(私は数日前に関する質問を)「コンピュータ」からタイピングの効果を作成する方法を持っているということですが、これはC++です。私はそれをC#に変換する方法を知っていますが、コードを正しく変換しても、作成するテキストにどのようにメソッドを適用すればよいですか?彼らはXNAでテキストを印刷するより効率的な方法ですか?

C++におけるタイピングの効果のためのコードは次のとおり

void typeOutput(string displayString){ 

     for(int i = 0; i < displayString.length(); i++){ 

      cout << displayString[i]; 
      Sleep((rand() + 1)%typeSpeed); 

     } 
    } 

答えて

1

this threadで議論されている。これを行うための様々な方法があります。そのスレッドからの一例は次のとおりです。

// our string will take 3 seconds to appear 
private const float timerLength = 3f; 
private float timer = 0f; 

は、その後、あなたのDrawメソッドであなたがタイマーに追加し、描画する文字列のどのくらいを決定するためにそれを使用:

timer += (float)gameTime.ElapsedGameTime.TotalSeconds; 

// if the timer is passed timerLength, we just draw the whole string 
if (timer >= timerLength) 
{ 
    spriteBatch.DrawString(myFont, myString, stringPosition, stringColor); 
} 

// otherwise we want to just draw a substring 
else 
{ 
    // figure out how many characters to show based on 
    // the ratio of the timer to the timerLength 
    int numCharsToShow = (int)(myString.Length * (timer/timerLength)); 
    string strToDraw = myString.Substring(0, numCharsToShow);  

    // now just draw the substring instead 
    spriteBatch.DrawString(myFont, strToDraw, stringPosition, stringColor); 
} 
+0

私はdidnの、そんなにありがとう実際にこの正確な答えを使用していませんが、私はあなたが必要とするすべてのものを満足させるリンク先の例を見つけました。テキストを生成し、タイピング効果を作り、テキストを中央に配置します。どうもありがとうございます! – Nikkisixx2

+0

あなたは大歓迎です! – keyboardP

関連する問題