在 Sliverlight 中使用 XNA,WPXNA(一)

平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛。在这里分享一下经验,仅为了和各位朋友交流经验。平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXNA 吧,最后请高手绕道而行吧,以免浪费时间。(为了突出重点和减少篇幅,有些示例代码可能不够严谨。)

XNA

好吧,现在 WP8 的游戏开发已经开始使用 DirectX/C++,如果你正在寻找新的编程方式,请关闭这个页面。

为什么使用 Sliverlight+XNA?因为有些朋友希望使用 Windows Phone 的控件,比如按钮,或者因为广告的插件不支持 XNA。

当然,在 WP7SDK 中,可以新建一个 Sliverlight+XNA 的项目,这里平方建立了一个 World 类(有些代码在默认的模板中也包含),以包含常用的代码,如下图所示:

在 Sliverlight 中使用 XNA,WPXNA(一)

在说明这个类之前,我们来看其他一些需要注意的问题。

修改 WMAppManifest.xml

在本示例中,WMAppManifest.xml 文件中的 NavigationPage 属性被修改为 World.xaml,这样游戏将把页面 World.xaml 作为启动页面。

在 Sliverlight 中使用 XNA,WPXNA(一)

修改命名空间

将 World.xaml 的命名空间修改为 zoyobar.game。这样,World.xaml 页面也就是我们的 World 类。

在 Sliverlight 中使用 XNA,WPXNA(一)

World 类

这个类将管理所有屏幕,比如:主菜单的屏幕,排名的屏幕等。当然,还包含其他一些全局性的功能。对于,今天这篇文章只包含了部分的代码,以后会不断添加。

循环

每一个游戏都需要一个循环,用来更新游戏相关的数据和绘制图形,所以这里我们使用 GameTimer 类。

1 private readonly GameTimer timer = new GameTimer ( );
 2 
 3 public World ( Color backgroundColor )
 4     : base ( )
 5 {
 6     // ...
 7 
 8     this.timer.UpdateInterval = TimeSpan.FromSeconds ( 1.0 / 30 );
 9     timer.Update += OnUpdate;
10     timer.Draw += OnDraw;
11 
12     // ...
13 }
14 
15 protected override void OnNavigatedTo ( NavigationEventArgs e )
16 {
17     // ...
18     
19     this.timer.Start ( );
20 
21     base.OnNavigatedTo ( e );
22 }

GameTimer 的 UpdateInterval 属性表示更新时间间隔,这里设置为 1 秒钟更新 30 次。而在 OnUpdate 和 OnDraw 方法中,我们可以编写用于更新和绘制的代码。方法 OnNavigatedTo 表示页面 World.xaml 已经载入,我们在这个方法中调用 GameTimer 的 Start 方法。

绘制

我们需要获取 GraphicsDevice,通过 GraphicsDevice 我们可以创建 SpriteBatch,使用 SpriteBatch 就可以绘制文字和图形了。

1 private SpriteBatch spiritBatch;
 2 internal readonly Color BackgroundColor;
 3 internal readonly ServiceProvider Services = new ServiceProvider ( Application.Current );
 4 internal readonly GraphicsDevice GraphicsDevice;
 5 
 6 public World ( Color backgroundColor )
 7     : base ( )
 8 {
 9     // ...
10 
11     SharedGraphicsDeviceManager graph = SharedGraphicsDeviceManager.Current;
12     this.GraphicsDevice = graph.GraphicsDevice;
13 
14     this.BackgroundColor = backgroundColor;
15     // ...
16 }
17 
18 protected override void OnNavigatedTo ( NavigationEventArgs e )
19 {
20     SharedGraphicsDeviceManager.Current.GraphicsDevice.SetSharingMode ( true );
21     this.spiritBatch = new SpriteBatch ( this.GraphicsDevice );
22     this.Services.AddService ( typeof ( SpriteBatch ), this.spiritBatch );
23     
24     // ...
25 
26     base.OnNavigatedTo ( e );
27 }

在构造函数中,我们获取了当前的 GraphicsDevice,并保存在字段 GraphicsDevice 中。当页面载入后,我们使用 GraphicsDevice 创建了 SpriteBatch,并将 SpriteBatch 保存到服务中,这是为了让其他类方便使用(但本示例中并没有用的)。

SetSharingMode 方法是比如调用的,将设备设置为共享,否则使用 SpriteBatch 绘制图像将没有效果。

Hello

最后,我们就来绘制一个字符串。首先我们需要获取字体,关于如何获取资源,以后会提到。之后,我们使用 SpriteBatch 的 DrawString 方法来绘制字符串。

1 private SpriteFont myfont;
 2 
 3 protected override void OnNavigatedTo ( NavigationEventArgs e )
 4 {
 5     // ...
 6 
 7     ContentManager content = new ContentManager ( this.Services, "Content" );
 8     this.myfont = content.Load<SpriteFont> ( @"fontmyfont" );
 9 
10     base.OnNavigatedTo ( e );
11 }
12 
13 private void OnDraw ( object sender, GameTimerEventArgs e )
14 {
15     this.GraphicsDevice.Clear ( this.BackgroundColor );
16 
17     this.spiritBatch.Begin ( );
18     this.spiritBatch.DrawString ( this.myfont, "Hello!", new Vector2 ( 10, 10 ), Color.White );
19     this.spiritBatch.End ( );
20 }

在上面的代码中,ContentManager 是需要调用 Unload 方法来释放资源的。示例中还需要一个 ServiceProvider 类,这里没有展示。

本期视频 http://v.youku.com/v_show/id_XNTYwNTE3NjE2.html

项目地址 http://wp-xna.googlecode.com/



更多内容 WPXNA

平方开发的游戏 http://zoyobar.lofter.com/

QQ 群 213685539

欢迎访问我在其他出发布的同一文章:http://www.wpgame.info/post/decc4_629f28
原文链接: https://www.cnblogs.com/zoyobar/archive/2013/05/22/wpxna1.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/89505

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月10日 上午12:11
下一篇 2023年2月10日 上午12:12

相关推荐