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

物品

玩家可以在游戏中获得各种物品,这样我们创建了一个新的类,用来表示物品。Item 类继承自 Spirit 类,他的很多内容和 Bullet 是类似的。

internal abstract class Item
: Spirit
{ private readonly bool isAutoPick; protected Item ( IPlayScene scene, int type, Vector2 location, string movieName, float speed, int angle, HitArea hitArea, int width, int height, double destroySecond, bool isAreaLimited, bool isAreaEntered, double areaSecond, bool isAutoPick )
: base ( scene, type, location, movieName,
null,
speed, angle, hitArea, width, height, destroySecond,
false,
isAreaLimited, isAreaEntered, areaSecond )
{
this.isAutoPick = isAutoPick; 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; } protected override void updateSpeed ( )
{
this.xSpeed = Calculator.Cos ( this.angle ) * this.speed;
this.ySpeed = Calculator.Sin ( this.angle ) * this.speed; base.updateSpeed ( );
} }

字段 isAutoPick 用来表示物品是否可以被自动拾取,不过目前并没有实现这个效果。方法 updateSpeed 用来根据字段 speed 来计算出 xSpeed 和 ySpeed。

至于其他的成员,大家可以参照 Bullet 类的解释。

物品管理器

ItemManager 类派生自类 SpiritManager<T>,默认的绘制顺序是 -1000。

事件 Picked 用来通知外界,物品已经被拾起,这时外界可以完成各种工作,比如:让玩家的生命值增加。

至于其他的成员,大家可以参照 BulletManager 类的解释。

internal class ItemManager
: SpiritManager<Item>
{
internal event EventHandler<HitAreaEventArgs> HitTesting;
internal event EventHandler<SpiritEventArgs> Picked; internal ItemManager ( )
: this ( - )
{ }
internal ItemManager ( int defaultOrder )
: base ( defaultOrder )
{ } internal override void Update ( GameTime time )
{ if ( null == this.HitTesting )
return; foreach ( Item item in this.Spirits.ToArray ( ) )
if ( null != item.HitArea )
{
HitAreaEventArgs hitAreaArg = new HitAreaEventArgs ( item.Type, item.HitArea );
this.HitTesting ( item, hitAreaArg ); if ( hitAreaArg.IsHit )
{ if ( null != this.Picked )
this.Picked ( item, new SpiritEventArgs ( item ) ); item.Destroy ( );
} } } }

示例

场景 SceneT17 是 SceneT16 的扩展,在 SceneT16 中,我们让子弹击中了小鸟,而在 SceneT17 中,我们还会创建物品,这些物品可以增加小鸟的生命值。

首先,我们修改了 Bird 类的 life 字段,将他给为可以被外界访问的。

internal class Bird
: Spirit, IAssailable
{
internal int Life = ; // ...
}

然后,我们定义了 MyItem 类,他就是新的物品,可以增加小鸟的生命值。

internal class MyItem
: Item
{ internal MyItem ( IPlayScene scene, Vector2 location, int angle )
: base ( scene, , location, "myitem", , angle,
new SingleRectangleHitArea ( new Rectangle ( -, -, , ) ),
, ,
,
true,
true,
,
true
)
{ } }

我们创建了新的 ItemManager,并设置了他的 HitTesting 和 Picked 事件。

在 itemHitTesting 方法中,我们将测试物品是否和小鸟发生了碰撞。在 itemPicked 方法中,我们为小鸟增加了生命值。

internal sealed class SceneT17
: CommandScene, IPlayScene
{
// ... private ItemManager itemManager; internal SceneT17 ( )
: 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 ( "go.image", ResourceType.Image, @"image\button1" ),
},
new Making[] {
new Movie ( "bird", "bird2.image", , , , "live",
new MovieSequence ( "live", true, new Point ( , ), new Point ( , ) )
),
new Movie ( "mybutton", "bullet.image", , , , "b",
new MovieSequence ( "b", new Point ( , ) )
),
new Movie ( "myitem", "item.image", , , , "i",
new MovieSequence ( "i", new Point ( , ) )
),
new Button ( "b.go", "go.image", "GO", new Vector2 ( , ), , , new Point ( , ) ),
}
)
{
// ... this.itemManager = new ItemManager ( );
this.itemManager.Scene = this;
this.itemManager.HitTesting += this.itemHitTesting;
this.itemManager.Picked += this.itemPicked; // ...
} private void itemHitTesting ( object sender, HitAreaEventArgs e )
{ if ( !this.bird.IsDied && e.HitArea.HitTest ( this.bird.HitArea ) )
e.IsHit = true; } private void itemPicked ( object sender, SpiritEventArgs e )
{
this.bird.Life++;
Debug.WriteLine ( "item picked, life={0}", this.bird.Life );
} // ...
}

最后,在点击按钮时,我们创建了物品。

private void goButtonSelected ( object sender, ButtonEventArgs e )
{
this.bulletManager.Append ( new MyBullet ( this, new Vector2 ( , ), ) );
this.itemManager.Append ( new MyItem ( this, new Vector2 ( , ), ) );
}

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

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

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

QQ 群 213685539

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

使用 Item,ItemManager 在 XNA 中创建物品和道具(十六)的更多相关文章

  1. 使用 Region,RegionManager 在 XNA 中创建特殊区域(十八)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  2. 使用 CommandScene 类在 XNA 中创建命令场景(十二)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  3. 使用 NPC,NPCManager 在 XNA 中创建 NPC

    使用 NPC,NPCManager 在 XNA 中创建 NPC 平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐 ...

  4. 使用 NPC,NPCManager 在 XNA 中创建 NPC(十九)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  5. 使用 Pinup,PinupManager 在 XNA 中创建贴图(十七)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  6. 使用 Bullet,BulletManager 在 XNA 中创建子弹攻击目标(十五)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  7. 使用 Spirit 类在 XNA 中创建游戏中的基本单位精灵(十三)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  8. 使用 Button 类在 XNA 中创建图形按钮(九)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

  9. 使用 Scene 类在 XNA 中创建不同的场景(八)

    平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...

随机推荐

  1. @Enable*注解的工作原理

    @EnableAspectJAutoProxy @EnableAsync @EnableScheduling @EnableWebMv @EnableConfigurationProperties @ ...

  2. 在MVC中加载view(点开链接)的方式

    主要有: Html.ActionLink Html.RenderPartial Html.RenderAction Html.Partial AJAX.ActionLink load 浏览器对象模型 ...

  3. [转贴] ASP.NET -- Web Service (.asmx) & JSON

    [转贴] ASP.NET -- Web Service (.asmx) & JSON 以前没做过,但临时被要求 ASP.NET Web Service 要传回 JSON格式 找到网络上两篇好文 ...

  4. 两数相除赋值整数变量(T-SQL)

    MSSQL: DECLARE @_pagecount INT; ; SELECT @_pagecount; 结果为1 Mysql: BEGIN DECLARE _pagecount INT; ; SE ...

  5. 一个SAP顾问在美国的这些年

    今天的文章来自我的老乡宋浩,之前作为SAP顾问在美国工作多年.如今即将加入SAP成都研究院S4CRM开发团队.我们都是大邑人. 大邑县隶属于四川省成都市,位于成都平原西部,与邛崃山脉接壤.东与崇州市交 ...

  6. 学习Rust Book之写Cargo配置文件

    不知道为什么这个文件就是编译不过 [package] name = "hello_world" version = "0.0.1" author = " ...

  7. [转载]AngularJS快速开始

    AngularJS快速开始 Hello World! 开始学习AngularJS的一个好方法是创建经典应用程序“Hello World!”: 使用您喜爱的文本编辑器,创建一个HTML文件,例如:hel ...

  8. 【BZOJ1057】[ZJOI2007] 棋盘制作(单调栈的运用)

    点此看题面 大致题意: 给你一个\(N*M\)的\(01\)矩阵,要求你分别求出最大的\(01\)相间的正方形和矩形(矩形也可以是正方形),并输出其面积. 题解 这题第一眼看去没什么思路,仔细想想,能 ...

  9. elasticsearch RestHighLevelClient 使用方法及封装工具

    目录 EsClientRHL 更新日志 开发原因: 使用前你应该具有哪些技能 工具功能范围介绍 工具源码结构介绍 开始使用 未来规划 git地址:https://gitee.com/zxporz/ES ...

  10. DeepLearning tutorial(3)MLP多层感知机原理简介+代码详解

    本文介绍多层感知机算法,特别是详细解读其代码实现,基于python theano,代码来自:Multilayer Perceptron,如果你想详细了解多层感知机算法,可以参考:UFLDL教程,或者参 ...