使用 Item,ItemManager 在 XNA 中创建物品和道具(十六)
平方已经开发了一些 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 中创建物品和道具(十六)的更多相关文章
- 使用 Region,RegionManager 在 XNA 中创建特殊区域(十八)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 CommandScene 类在 XNA 中创建命令场景(十二)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 NPC,NPCManager 在 XNA 中创建 NPC
使用 NPC,NPCManager 在 XNA 中创建 NPC 平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐 ...
- 使用 NPC,NPCManager 在 XNA 中创建 NPC(十九)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 Pinup,PinupManager 在 XNA 中创建贴图(十七)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 Bullet,BulletManager 在 XNA 中创建子弹攻击目标(十五)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 Spirit 类在 XNA 中创建游戏中的基本单位精灵(十三)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 Button 类在 XNA 中创建图形按钮(九)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 Scene 类在 XNA 中创建不同的场景(八)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
随机推荐
- tomcat jdk官网下载教程
Tomcat不同版本官网下载: 1.官网地址:http://tomcat.apache.org/ 2.点击要下载的版本进入下载页,点击Archives进入版本选择页,然后选择对应的版本文件夹,进去后点 ...
- nrm—源管理工具
全局安装 npm install -g nrm 查看可选源 nrm ls 其中,带*的是当前使用的源,上面的输出表明当前源是hiknpm 切换源 nrm use taobao 新增源 nrm add ...
- [转]超全!iOS 面试题汇总
转自:http://www.cocoachina.com/programmer/20151019/13746.html 1. Object-c的类可以多重继承么?可以实现多个接口么?Category是 ...
- 在SAP云平台的CloudFoundry环境下消费ABAP On-Premise OData服务
我的前一篇文章 使用Java+SAP云平台+SAP Cloud Connector调用ABAP On-Premise系统里的函数介绍了在SAP云平台的Neo环境下如何通过SAP Cloud Conne ...
- 微信小程序(底部导航的实现)
详情请看官方文档介绍: https://mp.weixin.qq.com/debug/wxadoc/dev/framework/config.html 在根目录配置文件app.json中配置底部导航: ...
- 【挖坑】2019年JAVA安全总结:SQL注入——新项目的开发与老项目的修复
如何在项目中有效的防止SQL注入 写给需要的人,所有的问题源自我们的不重视. 本章略过"什么是SQL注入","如何去利用SQL注入"的讲解,仅讲如何去防御 PS ...
- Python——函数入门(三)
一.变量作用域 当程序定义一个变量时,这个变量是有它的作用范围的,变量的作用范围称为变量的作用域.根据变量的位置,分为两种: 局部变量:局部变量就是在函数中定义的变量,包括参数,都是局部变量,局部离开 ...
- Bootstrap历练实例:警告框(Alert)插件的方法
<!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...
- RabbitMQ-消费者"未处理完的消息"丢失
一个关于客户端(消费者)开启自动应答,重启后"未处理消息丢失"的小坑.(主要是对RabbitMQ理解不够) 首先,申明一下: 本文所谓的 "丢失消息" 不是指服 ...
- 第六篇:python中numpy.zeros(np.zeros)的使用方法
用法:zeros(shape, dtype=float, order='C') 返回:返回来一个给定形状和类型的用0填充的数组: 参数:shape:形状 dtype:数据类型,可选参数,默认numpy ...