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

SpiritManager<T>

SpiritManager<T> 包含了一些管理精灵的方法,我们还可以派生很多的管理类,比如:子弹管理类。下面是 SpiritManager<T> 的成员。

字段 Spirits 表示管理器所管理的精灵,字段 defaultOrder 表示精灵的绘制次序。字段 Scene 可以让我们访问场景中的内容。以后,我们还会将其替换为 IPlayScene。

方法 Append 用来将精灵添加到管理器,我们将会精灵设置 Destroyed 事件,在这个事件中,被摧毁的精灵将被从管理器移除。在 remove 方法中,我们将注销 Destroyed 事件。

internal abstract class SpiritManager<T>
: IDisposable
where T : Spirit
{ internal readonly List<T> Spirits = new List<T> ( );
private readonly int defaultOrder; //internal IPlayScene Scene;
internal IScene Scene; protected SpiritManager ( )
: this ( )
{ }
protected SpiritManager ( int defaultOrder )
{ this.defaultOrder = defaultOrder; } protected virtual void spiritDestroyed ( object sender, SpiritEventArgs e )
{ this.remove ( sender as T ); } internal void Append ( IList<T> spirits )
{ if ( null != spirits )
foreach ( T spirit in spirits )
this.Append ( spirit ); } internal void Append ( T spirit )
{ this.Append ( spirit, this.defaultOrder ); } internal virtual void Append ( T spirit, int order )
{ spirit.Destroyed += this.spiritDestroyed; if ( order != && spirit.DrawOrder == )
spirit.DrawOrder = order; this.Spirits.Add ( spirit );
this.Scene.World.Components.Add ( spirit );
} private void remove ( T spirit )
{ spirit.Destroyed -= this.spiritDestroyed;
this.Scene.World.Components.Remove ( spirit );
this.Spirits.Remove ( spirit ); spirit.Dispose ( );
} internal virtual void Update ( GameTime time )
{ } internal void RemoveAll ( )
{ T[] spirits = this.Spirits.ToArray ( ); foreach ( T spirit in spirits )
this.remove ( spirit ); } public virtual void Dispose ( )
{ this.RemoveAll ( ); } }

示例

场景 SceneT15 和之前的 SceneT14 是类似,我们同样有一个 Bird 类,但是我们增加了一个 BirdManager 类,这个类从 SpiritManager<T> 派生。

BirdManager 有两个方法,Go 方法将调用所有的 Bird 类的 Go 方法,Stop 方法将调用所有的 Bird 类的 Stop 方法。

internal class BirdManager
: SpiritManager<Bird>
{ internal BirdManager ( )
: base ( )
{ } internal void Go ( )
{ foreach ( Bird bird in this.Spirits )
bird.Go ( ); } internal void Stop ( )
{ foreach ( Bird bird in this.Spirits )
bird.Stop ( ); } }

SceneT15 同样包含了两个按钮,当点击两个按钮时,将分别调用 BirdManager 的 Go 和 Stop 方法。在 SceneT15 构造函数中,我们创建 BirdManager 类,并需要设置字段 Scene。

在 LoadContent 方法中,我们将两个 Bird 类添加到 BirdManager 中。而在 UnloadContent 方法中,我们调用 BirdManager 的 RemoveAll 方法来删除所有的小鸟。

在 Dispose 方法中,我们将调用 BirdManager 的 Dispose 方法。

internal sealed class SceneT15
: CommandScene
{
private BirdManager birdManager;
private readonly Button goButton;
private readonly Button stopButton; internal SceneT15 ( )
: base ( Vector2.Zero, GestureType.None, "background1",
new Resource[] {
new Resource ( "bird2.image", ResourceType.Image, @"image\bird2" ),
new Resource ( "go.image", ResourceType.Image, @"image\button1" ),
new Resource ( "stop.image", ResourceType.Image, @"image\button2" ),
},
new Making[] {
new Movie ( "bird", "bird2.image", , , , "stop",
new MovieSequence ( "go", true, new Point ( , ), new Point ( , ) ),
new MovieSequence ( "stop", true, new Point ( , ) )
),
new Button ( "b.go", "go.image", "GO", new Vector2 ( , ), , , new Point ( , ) ),
new Button ( "b.play", "stop.image", "STOP", new Vector2 ( , ), , , new Point ( , ) )
}
)
{
this.birdManager = new BirdManager ( );
this.birdManager.Scene = this; this.goButton = this.makings[ "b.go" ] as Button;
this.stopButton = this.makings[ "b.play" ] as Button; this.goButton.Selected += this.goButtonSelected;
this.stopButton.Selected += this.stopButtonSelected;
} private void goButtonSelected ( object sender, ButtonEventArgs e )
{ this.birdManager.Go ( ); } private void stopButtonSelected ( object sender, ButtonEventArgs e )
{ this.birdManager.Stop ( ); } public override void LoadContent ( )
{
base.LoadContent ( ); this.birdManager.Append ( new Bird ( this, new Vector2 ( , ) ) );
this.birdManager.Append ( new Bird ( this, new Vector2 ( , ) ) );
} public override void UnloadContent ( )
{
this.birdManager.RemoveAll ( ); base.UnloadContent ( );
} public override void Dispose ( )
{
this.birdManager.Dispose ( ); base.Dispose ( );
} }

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

项目地址 http://wp-xna.googlecode.com/
更多内容 WPXNA

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

QQ 群 213685539

欢迎访问我在其他位置发布的同一文章:http://www.wpgame.info/post/decc4_76f27b

最新文章

  1. SQLSERVER 获取datetime日期的查询语句
  2. Hidden File For Mac
  3. mysql处理海量数据时的一些优化查询速度方法
  4. html格式化
  5. JQuery获取页面关闭事件
  6. UISegmentedControl(转)
  7. JSP中两种模式的总结
  8. Java开发中文件读取方式总结
  9. MongoDB C driver API continues
  10. kindeditor编辑器,图片上传功能齐全
  11. TOMCAT的框架结构
  12. Redis基础一(Linux)
  13. Hadoop Yarn 框架原理及运作机制及与MapReduce比较
  14. ERROR 1044 (42000): Access denied for user &#39;root&#39;@&#39;%&#39; to database &#39;mysql&#39;
  15. php xml格式对象 返回-&gt;对应格式数组
  16. Scala微服务架构 三
  17. 集合List
  18. linux查询系统负载
  19. 经纬度 lbs 笔记
  20. 屏幕置顶(WindowManager服务)

热门文章

  1. Eucalyptus-利用镜像启动一个Centos实例
  2. maven-整合到eclips
  3. sk-learning(2)
  4. IIS7.5配置自动添加www 及 限制通过IP访问web
  5. hdu-1874 畅通工程续---模板题
  6. 【BZOJ4300】绝世好题(位运算水题)
  7. 管理员必备的几个Linux系统监控工具
  8. java 字符串中是否有数字
  9. java arraylist int[] 转换
  10. 20180909 解析JS Cookie的设置,获取和检索