平方已经开发了一些 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 ( 0 ) { } 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 != 0 && spirit.DrawOrder == 0 ) 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", 80, 80, 5, "stop", new MovieSequence ( "go", true, new Point ( 1, 1 ), new Point ( 2, 1 ) ), new MovieSequence ( "stop", true, new Point ( 3, 1 ) ) ), new Button ( "b.go", "go.image", "GO", new Vector2 ( 100, 100 ), 100, 50, new Point ( 1, 1 ) ), new Button ( "b.play", "stop.image", "STOP", new Vector2 ( 100, 300 ), 100, 50, new Point ( 1, 1 ) ) } ) { 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 ( 200, 100 ) ) ); this.birdManager.Append ( new Bird ( this, new Vector2 ( 300, 200 ) ) ); } 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/