日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

使用 Label 类在 XNA 中显示文本,WPXNA(七)

發布時間:2024/1/17 编程问答 42 豆豆
生活随笔 收集整理的這篇文章主要介紹了 使用 Label 类在 XNA 中显示文本,WPXNA(七) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

平方已經開發了一些 Windows Phone 上的一些游戲,算不上什么技術大牛。在這里分享一下經驗,僅為了和各位朋友交流經驗。平方會逐步將自己編寫的類上傳到托管項目中,沒有什么好名字,就叫 WPXNA 吧,最后請高手繞道而行吧,以免浪費時間。(為了突出重點和減少篇幅,有些示例代碼可能不夠嚴謹。)

標簽

在游戲中,我們需要向用戶顯示一些文字信息,比如:玩家的名字,分數等。這時候,可以使用 Label 類。Label 類繼承自 Making,所以他是一個元件。以下是 Label 中的部分字段和屬性:

private float blink; internal float Alpha = 1; internal readonly float Rotation;internal string Text; internal float FontScale; protected SpriteFont font; protected Color color;protected Vector2 location; public Vector2 Location {get { return this.location; }set { this.location = value; } }

字段 Alpha 表示標簽的透明度,1 表示不透明,0 表示完全透明。字段 blink 的值應該在 -1 到 0 之間,如果 blink 不為 0,則標簽會閃爍。

字段 Rotation 表示標簽的旋轉角度,屬性 Location 表示標簽的位置。

字段 Text 表示標簽的文本,字段 FontScale 表示文本的縮放比例,字段 font 表示文本的字體,字段 color 表示文本的顏色。

internal Label ( string name, string resourceName, string text, Vector2 location, int width, int height, float fontScale, Color color, float blink, float alpha, int angle ): base ( name, resourceName ) {if ( null == text )throw new ArgumentNullException ( "text", "text can't be null" );if ( width > 0 )this.Width = width;if ( height > 0 )this.Height = height;this.Text = text;this.location = location;this.FontScale = fontScale <= 0 ? 1 : fontScale;this.color = color;this.blink = blink;this.Alpha = alpha < 0 || alpha > 1 ? 1 : alpha;this.Rotation = Calculator.Radian ( angle ); }

在 Label 的構造函數中,除了上面提到的字段,參數 name 表示元件的名稱,參數 resourceName 表示標簽使用的字體資源,參數 width 和 height 為標簽的大小,可以忽略。你可以調用方法 InitSize 來獲取標簽的大小。

internal static void InitSize ( Label label, bool isForce ) {if ( null == label )return;if ( label.Width == 0 || isForce )label.Width = ( int ) ( label.font.MeasureString ( label.Text ).X * label.FontScale );if ( label.Height == 0 || isForce )label.Height = ( int ) ( label.font.LineSpacing * label.FontScale );}

你可以通過 Draw 方法來繪制標簽。

internal static void Draw ( Label label, SpriteBatch batch ) {if ( !label.isVisible )return;Color color = label.color;if ( label.blink != 0 ){label.Alpha += label.blink;if ( label.Alpha <= 0.5 || label.Alpha >= 1 )label.blink = -label.blink;}if ( label.Alpha != 1 )color = color * label.Alpha;batch.DrawString ( label.font, label.Text, label.location * World.Scale, color, label.Rotation, Vector2.Zero, label.FontScale * ( label.Rotation == 0 ? World.Scale : World.FlipScale ), SpriteEffects.None, 0 ); }

在 Draw 方法中,我們將根據字段 blink 來不斷的調整 Alpha 字段,也就是標簽的透明度,這樣標簽的透明度將在 0.5 和 1 之間改變。

一個簡單的例子

首先,我們需要使用 ResourceManager 來管理資源,另外,我們定義了兩個標簽。

private readonly ResourceManager resourceManager; private readonly Label label1; private readonly Label label2;

在構造函數中,我們初始化了 ResourceManager 和 Label,ResourceManager 將包含一個字體資源,他包含在資源項目的 font 目錄中,字體資源被命名為 peg。

之后,我們創建了兩個標簽。第一個標簽是淺綠色,字體縮放大小為 2 倍,第二個標簽是垂直的,并且可以閃爍。

public World ( Color backgroundColor ): base ( ) {// ...this.resourceManager = new ResourceManager ( new Resource[] {new Resource ( "peg", ResourceType.Font, @"font\myfont" )} );this.resourceManager.World = this;this.label1 = new Label ( "l1", "Hello windows phone!", 2f, Color.LightGreen, 0f );this.label2 = new Label ( "l2", "peg", "Nothing!", new Vector2 ( 50, 300 ), 0, 0, 1f, Color.White, -0.01f, 1f, -90 ); }

當頁面載入之后,我們加載所需要的資源。

protected override void OnNavigatedTo ( NavigationEventArgs e ) {// ...this.resourceManager.LoadContent ( );this.label1.InitResource ( this.resourceManager );this.label2.InitResource ( this.resourceManager );base.OnNavigatedTo ( e ); }

在 OnUpdate 方法中,我們讓第二個標簽顯示游戲進行的時間。

private void OnUpdate ( object sender, GameTimerEventArgs e ) {this.label2.Text = e.TotalTime.ToString ( ); }

在 OnDraw 中,我們通過 Label 的 Draw 方法繪制了兩個標簽。

private void OnDraw ( object sender, GameTimerEventArgs e ) {// ...this.spiritBatch.Begin ( );Label.Draw ( this.label1, this.spiritBatch );Label.Draw ( this.label2, this.spiritBatch );this.spiritBatch.End ( ); }

?

本期視頻 http://v.youku.com/v_show/id_XNTY3MzA2MTk2.html
項目地址 http://wp-xna.googlecode.com/

更多內容 WPXNA
平方開發的游戲 http://zoyobar.lofter.com/
QQ 群 213685539

歡迎訪問我在其他位置發布的同一文章:http://www.wpgame.info/post/decc4_68994c

轉載于:https://www.cnblogs.com/zoyobar/archive/2013/06/07/wpxna7.html

總結

以上是生活随笔為你收集整理的使用 Label 类在 XNA 中显示文本,WPXNA(七)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。