
※この Tips は XNA Game Studio 2.0 をベースに説明しています。
テキストを回転させて表示するには「SpriteBatch.DrawString」メソッドの第5引数に回転角度を 「radian」で指定します。一般的に使われている 0~360 の数値の単位は「degree」と呼ばれ「radian」は 0~2π の値になります。
サンプルでは、一般的に認識しやすい degree の単位を使い「MathHelper.ToRadians」メソッドによって数値を radian に変換して引数に渡しています。
// 時計回りに45度回転させて描画 this.spriteBatch.DrawString(this.font, "Draw rotate text (45 degree).", new Vector2(300.0f, 300.0f), Color.White, MathHelper.ToRadians(45.0f), Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f);
| SpriteBatch.DrawString メソッド | ||
|---|---|---|
| 文字列を描画します。 | ||
| spriteFont | SpriteFont | 文字列のイメージが格納されている SpriteFont を指定します。 |
| text | string | 表示するテキストを指定します。 |
| position | Vector2 | テキストを表示する位置。スクリーンの左上を基準にしたスクリーン座標で指定します。テキストの原点は最初の文字の左上になります。 |
| color | Color | テキストの色 |
| rotation | float | テキストの回転角度。単位は radian で指定する。回転軸はテキストの左上になります。 |
| origin | Vector2 | テキストを回転させるときの回転軸の位置を指定します。テキストのどの位置を回転軸にするかを指定しますが、実際には回転軸の位置はテキストの左上固定で、テキストの表示位置が -origin 移動します。 |
| scale | float | テキストの拡大率を指定します。1.0を基準として、縦と横に拡大縮小します。拡大の原点はテキストの左上になります。 |
| effects | SpriteEffects | テキストの表示反転効果を指定します。特に何もしなければ SpriteEffects.None を指定します。 |
| layerDepth | float | テキストを表示する深度を指定します。主にテキストを手前に表示したり奥に表示するのに使用します。0.0~1.0 の範囲で指定し、0.0が最前面、1.0が最背面になります。 |
| MSDN ライブラリの Web ページへリンク | ||
| MathHelper.ToRadians メソッド | ||
|---|---|---|
| degree の値を radian に変換します。変換式は「radian = degree * π / 180.0」です。 | ||
| degrees | float | radian に変換する degree の値を指定します。 |
| 戻り値 | float | degree の値を radian に変換した値を返します。 |
| MSDN ライブラリの Web ページへリンク | ||
回転軸はデフォルトでテキストの左上の位置になります。

| ファイル | サイズ | 対応XNAバージョン | プラットフォーム | 作成日 |
|---|---|---|---|---|
| xna_tips_rotatetext_3_0_exe.zip | 13.4 KB | 3.0 | Windows (XP SP2 以降, Vista) | 2009/01/04 |
| xna_tips_rotatetext_2_0_exe.zip | 15.9 KB | 2.0 | Windows (XP SP2, Vista) | 2008/01/01 |
| xna_tips_rotatetext_1_0_ref_exe.zip | 16.1 KB | 1.0 Refresh | Windows (XP SP2, Vista) | 2007/07/17 |
| ファイル | サイズ | 対応XNAバージョン | プラットフォーム | 作成日 |
|---|---|---|---|---|
| xna_tips_rotatetext_3_0_project.zip | 17.8 KB | 3.0 | Windows (XP SP2 以降, Vista), Xbox 360 | 2009/01/04 |
| xna_tips_rotatetext_2_0_project.zip | 16.2 KB | 2.0 | Windows (XP SP2, Vista), Xbox 360 | 2008/01/01 |
| xna_tips_rotatetext_1_0_ref_project.zip | 16.6 KB | 1.0 Refresh | Windows (XP SP2, Vista), Xbox 360 | 2007/07/17 |
using System; using System.Collections.Generic; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Net; using Microsoft.Xna.Framework.Storage; namespace RotateText { /// <summary> /// ゲームメインクラス /// </summary> public class GameMain : Microsoft.Xna.Framework.Game { /// <summary> /// グラフィックデバイス管理クラス /// </summary> private GraphicsDeviceManager graphics = null; /// <summary> /// スプライトのバッチ化クラス /// </summary> private SpriteBatch spriteBatch = null; /// <summary> /// スプライトでテキストを描画するためのフォント /// </summary> private SpriteFont font = null; /// <summary> /// リアルタイムに増加する回転量 /// </summary> private float realRotate = 0.0f; /// <summary> /// GameMain コンストラクタ /// </summary> public GameMain() { // グラフィックデバイス管理クラスの作成 this.graphics = new GraphicsDeviceManager(this); // ゲームコンテンツのルートディレクトリを設定 this.Content.RootDirectory = "Content"; } /// <summary> /// ゲームが始まる前の初期化処理を行うメソッド /// グラフィック以外のデータの読み込み、コンポーネントの初期化を行う /// </summary> protected override void Initialize() { // TODO: ここに初期化ロジックを書いてください // コンポーネントの初期化などを行います base.Initialize(); } /// <summary> /// ゲームが始まるときに一回だけ呼ばれ /// すべてのゲームコンテンツを読み込みます /// </summary> protected override void LoadContent() { // テクスチャーを描画するためのスプライトバッチクラスを作成します this.spriteBatch = new SpriteBatch(this.GraphicsDevice); // フォントをコンテンツパイプラインから読み込む this.font = this.Content.Load<SpriteFont>("Font"); } /// <summary> /// ゲームが終了するときに一回だけ呼ばれ /// すべてのゲームコンテンツをアンロードします /// </summary> protected override void UnloadContent() { // TODO: ContentManager で管理されていないコンテンツを // ここでアンロードしてください } /// <summary> /// 描画以外のデータ更新等の処理を行うメソッド /// 主に入力処理、衝突判定などの物理計算、オーディオの再生など /// </summary> /// <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param> protected override void Update(GameTime gameTime) { // Xbox360 コントローラの BACK ボタンを押したときにゲームを終了させます if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) { this.Exit(); } // 回転量を増やす this.realRotate += 0.5f; // 登録された GameComponent を更新する base.Update(gameTime); } /// <summary> /// 描画処理を行うメソッド /// </summary> /// <param name="gameTime">このメソッドが呼ばれたときのゲーム時間</param> protected override void Draw(GameTime gameTime) { // 画面を指定した色でクリアします this.GraphicsDevice.Clear(Color.CornflowerBlue); // スプライトの描画準備 this.spriteBatch.Begin(); // 普通に描画 this.spriteBatch.DrawString(this.font, "Draw rotate text (0 degree).", new Vector2(300.0f, 300.0f), Color.White, MathHelper.ToRadians(0.0f), Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f); // 時計回りに45度回転させて描画 this.spriteBatch.DrawString(this.font, "Draw rotate text (45 degree).", new Vector2(300.0f, 300.0f), Color.White, MathHelper.ToRadians(45.0f), Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f); // 自動で回転させて描画 this.spriteBatch.DrawString(this.font, "Draw rotate text (" + ((int)this.realRotate).ToString() + " degree).", new Vector2(300.0f, 300.0f), Color.White, MathHelper.ToRadians(this.realRotate), Vector2.Zero, 1.0f, SpriteEffects.None, 0.0f); // スプライトの一括描画 this.spriteBatch.End(); // 登録された DrawableGameComponent を描画する base.Draw(gameTime); } } }
| 更新日時 | 更新内容 |
|---|---|
| 2009/01/04 | XNA Game Studio 3.0 のプロジェクト追加 |
| 2008/05/18 | 文章・プログラムの校正 |
| 2008/01/01 | XNA Game Studio 2.0 用に修正 |
| 2007/07/17 | ページ作成 |