平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛。在这里分享一下经验,仅为了和各位朋友交流经验。平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXNA 吧,最后请高手绕道而行吧,以免浪费时间。(为了突出重点和减少篇幅,有些示例代码可能不够严谨。)
贴图
在游戏中,贴图可以用来显示文字或者图片等内容。比如:显示“好”,“太棒了”,也可以用来显示生命条。
我们的定义了一个 Pinup 类来表示贴图,他是一个很简单的类。
internal abstract class Pinup : Spirit { protected Pinup ( IPlayScene scene, int type, Vector2 location, string movieName, float speed, int angle, int width, int height, double destroySecond ) : base ( scene, type, location, movieName, null, speed, angle, null, width, height, destroySecond, true, false, false, 0 ) { } protected override Vector2 getMovieLocation ( ) { return this.Location - this.halfSize; } protected override void updateSpeed ( ) { this.xSpeed = Calculator.Cos ( this.angle ) * this.speed; this.ySpeed = Calculator.Sin ( this.angle ) * this.speed; base.updateSpeed ( ); } protected override void move ( ) { this.Location.X += this.xSpeed; this.Location.Y += this.ySpeed; } }由于这个类中没有特别的成员,所以大家可以参照其他 Spirit 类的解释。
贴图管理器
PinupManager 类派生自类 SpiritManager<T>,默认的绘制顺序是 4000。
internal class PinupManager : SpiritManager<Pinup> { internal PinupManager ( ) : base ( 4000 ) { } }
示例
场景 SceneT18 是 SceneT17 的扩展,除了有 SceneT17 的功能,还将显示一个贴图。
我们定义了类 MyHit 类,他表示一个贴图,当小鸟第一次被子弹击中后,我们将显示 MyHit。
internal class MyHit : Pinup { internal MyHit ( IPlayScene scene, Vector2 location ) : base ( scene, 1, location, "mypinup", 0, 0, 30, 30, 1 ) { } }然后,我们定义了一些字段。
private PinupManager pinupManager; private bool is1Hit = false;字段 is1Hit 用来表示小鸟是否被第一次击中。
internal SceneT18 ( ) : 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 ( "item.image", ResourceType.Image, @"image\item" ), new Resource ( "pinup.image", ResourceType.Image, @"image\pinup1" ), 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 Movie ( "myitem", "item.image", 30, 30, 0, "i", new MovieSequence ( "i", new Point ( 1, 1 ) ) ), new Movie ( "mypinup", "pinup.image", 100, 50, 0, "p", new MovieSequence ( "p", new Point ( 1, 1 ) ) ), new Button ( "b.go", "go.image", "GO", new Vector2 ( 10, 690 ), 100, 50, new Point ( 1, 1 ) ), } ) { // ... this.pinupManager = new PinupManager ( ); this.pinupManager.Scene = this; // ... }在 SceneT18 的构造函数中,我们将创建了类 PinupManager。
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 }; if ( !this.is1Hit ) { this.is1Hit = true; this.pinupManager.Append ( new MyHit ( this, new Vector2 ( 0, 400 ) ) ); } } }我们根据字段 is1Hit 来决定是否显示贴图,而 MyHit 的显示时间为 1 秒。
本期视频 http://v.youku.com/v_show/id_XNTg4NDI0NDA0.html
项目地址 http://wp-xna.googlecode.com/