平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛。在这里分享一下经验,仅为了和各位朋友交流经验。平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXNA 吧,最后请高手绕道而行吧,以免浪费时间。(为了突出重点和减少篇幅,有些示例代码可能不够严谨。)
子弹
子弹在游戏中很重要,几乎所有的游戏都有子弹或者类似子弹的东西。下面我们来介绍一下 Bullet 类。
Bullet 类派生自 Spirit 类,下面是他的字段:
internal readonly int Power; private int life;字段 Power 表示子弹的威力,字段 life 表示子弹的生命值。子弹每一次攻击目标都将扣除自己的生命值,如果生命值小于等于 0,则子弹将被销毁。如果子弹初始生命值为 0,则表示生命值无限。
Bullet 类的构造函数的参数和 Spirit 类似,字段 isMoving 被设置为 true,这表示子弹一开始就处于移动状态,不过你需要设置子弹的移动速度。
protected Bullet ( /* IPlayScene scene */ IScene scene, int type, Vector2 location, string movieName, float speed, int angle, HitArea hitArea, int width, int height, int power, int life, double destroySecond, bool isMovieRotable, bool isAreaLimited, bool isAreaEntered, double areaSecond ) : base ( scene, type, location, movieName, null, speed, angle, hitArea, width, height, destroySecond, isMovieRotable, isAreaLimited, isAreaEntered, areaSecond ) { this.Power = power < 0 ? 0 : power; this.life = life; this.isMoving = true; } protected override void move ( ) { this.Location.X += this.xSpeed; this.Location.Y += this.ySpeed; } protected override Vector2 getMovieLocation ( ) { return this.Location - this.halfSize; }在方法 move 中,我们根据 xSpeed 和 ySpeed 来移动子弹的位置。方法 getMovieLocation 中,我们设置了电影的位置,这样使电影的中心和子弹的中心一致。
internal virtual void Attack ( IList<IAssailable> targets ) { if ( null == targets ) return; foreach ( IAssailable target in targets ) { target.Injure ( this.Power, this.type ); if ( this.life > 0 ) { this.life -= this.Power; if ( this.life <= 0 ) { this.Destroy ( ); break; } } } }方法 Attack 可以让子弹攻击一些目标,这样目标都需要实现 IAssailable 接口。我们不会直接扣除目标的生命值,而是调用他们的 Injure 方法,这样每一个目标都可以完成自己的任务。
子弹管理器
BulletManager 类派生自类 SpiritManager<T>,默认的绘制顺序是 2000。
HitTesting 事件用来让外界测试碰撞,通过参数 BulletHitAreaEventArgs,BulletManager 可以知道子弹是否击中了目标,如果子弹击中了目标,则将调用子弹的 Attack 方法。
我们需要调用字段 Spirits 的 ToArray 方法,因为 Spirits 在循环中是可能变化的。
internal event EventHandler<BulletHitAreaEventArgs> HitTesting; internal BulletManager ( ) : base ( 2000 ) { } internal override void Update ( GameTime time ) { if ( null == this.HitTesting ) return; foreach ( Bullet bullet in this.Spirits.ToArray ( ) ) if ( null != bullet.HitArea ) { BulletHitAreaEventArgs hitAreaArg = new BulletHitAreaEventArgs ( bullet ); this.HitTesting ( bullet, hitAreaArg ); if ( hitAreaArg.IsHit ) bullet.Attack ( hitAreaArg.Targets ); hitAreaArg.Dispose ( ); } }在本次的示例中,方法 HitTest 并没有调用,该方法将返回和参数 area 发生碰撞的子弹。
internal List<Bullet> HitTest ( HitArea area, int mode ) { List<Bullet> bullets = new List<Bullet> ( ); foreach ( Bullet bullet in this.Spirits ) if ( mode == 1 && bullet.Type < 100 && area.HitTest ( bullet.HitArea ) ) bullets.Add ( bullet ); else if ( mode == 2 && bullet.Type >= 100 && area.HitTest ( bullet.HitArea ) ) bullets.Add ( bullet ); return bullets; }
示例
场景 SceneT16 中,点击按钮将创建子弹并击中小鸟。
Bird 类在前面的示例中出现过,我们为他实现了 IAssailable 接口,在 Injure 方法中,我们扣除小鸟的生命值,如果生命值小于等于 0,则销毁小鸟并将字段 IsDied 设置为 true。
internal class Bird : Spirit, IAssailable { private int life = 10; internal bool IsDied = false; internal Bird ( IScene scene, Vector2 location ) : base ( scene, 0, location, "bird", null, 4, 0, new SingleRectangleHitArea ( new Rectangle ( -40, -40, 80, 80 ) ), 80, 80, 0, true, false, false, 0 ) { } public bool Injure ( int life, int type ) { this.life -= life; Debug.WriteLine ( "life={0}", this.life ); if ( this.life <= 0 ) { Debug.WriteLine ( "life<0" ); this.IsDied = true; this.Destroy ( ); } return true; } protected override Vector2 getMovieLocation ( ) { return this.Location - this.halfSize; } }类 MyBullet 派生自 Bullet,我们修改了 updateSpeed 方法,用来设置 xSpeed 和 ySpeed。
internal class MyBullet : Bullet { internal MyBullet ( IScene scene, Vector2 location, int angle ) : base ( scene, 100, location, "mybutton", 5, angle, new SingleRectangleHitArea ( new Rectangle ( -5, -5, 10, 10 ) ), 10, 10, 2, 2, 0, false, true, true, 0 ) { } protected override void updateSpeed ( ) { this.xSpeed = Calculator.Cos ( this.angle ) * this.speed; this.ySpeed = Calculator.Sin ( this.angle ) * this.speed; base.updateSpeed ( ); } }在按钮的 Selected 方法,我们将调用 BulletManager 的 Append 方法来创建并添加 MyBullet,这样就产生了新的子弹。
方法 bulletHitTesting 用来判断子弹是否击中了小鸟。参数 e 的 HitArea 字段表示子弹的碰撞区域,设置 e 的 IsHit 字段为 true,则表示发生了碰撞,e 的 Targets 字段表示目标。
internal sealed class SceneT16 : CommandScene { // ... private Bird bird; private BirdManager birdManager; private BulletManager bulletManager; private readonly Button goButton; internal SceneT16 ( ) : base ( Vector2.Zero, GestureType.None, "background1", new Resource[] { new Resource ( "bird2.image", ResourceType.Image, @"image\bird2" ), new Resource ( "bullet.image", ResourceType.Image, @"image\bullet" ), new Resource ( "go.image", ResourceType.Image, @"image\button1" ), }, new Making[] { new Movie ( "bird", "bird2.image", 80, 80, 5, "live", new MovieSequence ( "live", true, new Point ( 1, 1 ), new Point ( 2, 1 ) ) ), new Movie ( "mybutton", "bullet.image", 10, 10, 0, "b", new MovieSequence ( "b", new Point ( 1, 1 ) ) ), new Button ( "b.go", "go.image", "GO", new Vector2 ( 10, 690 ), 100, 50, new Point ( 1, 1 ) ), } ) { this.birdManager = new BirdManager ( ); this.birdManager.Scene = this; this.bulletManager = new BulletManager ( ); this.bulletManager.Scene = this; this.bulletManager.HitTesting += this.bulletHitTesting; this.goButton = this.makings[ "b.go" ] as Button; this.goButton.Selected += this.goButtonSelected; } private void bulletHitTesting ( object sender, BulletHitAreaEventArgs e ) { if ( !this.bird.IsDied && e.HitArea.HitTest ( this.bird.HitArea ) ) { e.IsHit = true; e.Targets = new IAssailable[] { this.bird }; } } private void goButtonSelected ( object sender, ButtonEventArgs e ) { this.bulletManager.Append ( new MyBullet ( this, new Vector2 ( 10, 10 ), 45 ) ); } protected override void updating ( GameTime time ) { this.bulletManager.Update ( time ); base.updating ( time ); } public override void LoadContent ( ) { base.LoadContent ( ); this.bird = new Bird ( this, new Vector2 ( 200, 200 ) ); this.birdManager.Append ( this.bird ); } public override void UnloadContent ( ) { this.birdManager.RemoveAll ( ); this.bulletManager.RemoveAll ( ); base.UnloadContent ( ); } public override void Dispose ( ) { this.birdManager.Dispose ( ); this.bulletManager.HitTesting -= this.bulletHitTesting; this.bulletManager.Dispose ( ); this.goButton.Selected -= this.goButtonSelected; base.Dispose ( ); } }
本期视频 http://v.youku.com/v_show/id_XNTg2ODM0MDQ0.html
项目地址 http://wp-xna.googlecode.com/