使用 CommandScene 类在 XNA 中创建命令场景(十二)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛。在这里分享一下经验,仅为了和各位朋友交流经验。平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXNA 吧,最后请高手绕道而行吧,以免浪费时间。(为了突出重点和减少篇幅,有些示例代码可能不够严谨。)
CommandScene
CommandScene 类继承自 Scene 类,在这个场景中,我们将控制按钮并检测按钮的点击情况,而且可以将这些情况传递给外界。
internal event EventHandler<SceneEventArgs> Executing; private readonly Shape backgroundShape; protected readonly List<Button> buttons = new List<Button> ( );
private readonly List<Anime> animes = new List<Anime> ( );事件 Executing 将通知外界哪些按钮被点击了,字段 buttons 表示被 CommandScene 控制的所有按钮。
字段 backgroundShape 表示场景的背景图片,字段 animes 表示场景中的动画。
场景 CommandScene 将包含一些资源,包括:字体,背景图片,按钮点击的声音。
internal CommandScene ( Vector2 location, GestureType gestureType, string backgroundResourcePath, IList<Resource> resources, IList<Making> makings, bool isBroken )
: base ( location, gestureType,
ResourceManager.Combine ( new Resource[] {
new Resource ( "peg", ResourceType.Font, @"font\peg" ),
new Resource ( "background", ResourceType.Image, string.Format ( @"image\{0}", backgroundResourcePath ) ),
new Resource ( "click.s", ResourceType.Sound, @"sound\click" ),
}, resources ),
combine ( new Making[] {
new Shape ( "background.s", "background" )
}, makings ),
isBroken
)
{
this.backgroundShape = this.makings["background.s"] as Shape; foreach ( Making making in this.makings.Values )
if ( making is Button )
{
Button button = making as Button;
button.Selected += this.buttonSelected;
this.buttons.Add ( button );
}
else if ( making is Anime )
this.animes.Add ( making as Anime ); } public override void Dispose ( )
{ foreach ( Button button in this.buttons )
button.Selected -= this.buttonSelected; this.buttons.Clear ( );
this.animes.Clear ( ); base.Dispose ( );
}在构造函数中,我们从元件中获取所有的 Button 对象,并设置他们的 Selected 事件。而在 Dispose 方法中,我们做了相反的操作。
private void buttonSelected ( object sender, ButtonEventArgs e )
{ this.Execute ( e.Command ); } internal void Execute ( string command )
{ if ( null != this.Executing )
this.Executing ( this, new SceneEventArgs ( command ) ); }每一个按钮的 Selected 事件都将执行 buttonSelected 方法,在该方法中,我们将调用 Execute 方法。
在方法 Execute 中,我们将触发 CommandScene 的 Executing 事件,参数 command 和按钮的 command 字段相同,这样外界就可以知道是哪个按钮被点击了。如果按钮的 IsSole 字段为 true,则只有一个按钮的点击是有效的。
protected override void inputing ( Controller controller )
{ foreach ( Button button in this.buttons )
Button.PressTest ( button, controller.Motions ); foreach ( Button button in this.buttons )
if ( Button.ClickTest ( button, controller.Motions ) && button.IsSole )
break; }在 inputing 方法中,我们同时调用了 Button 的 PressTest 和 ClickTest 方法,这样可以检测按钮的按下和点击情况。但 PressTest 是给以后的场景使用的,这里我们用的是 ClickTest,他将检测按钮的点击情况并触发 Button 的 Selected。
protected override void drawing ( GameTime time, SpriteBatch batch )
{
Shape.Draw ( this.backgroundShape, time, batch ); foreach ( Anime anime in this.animes )
Anime.Draw ( anime, time, batch ); foreach ( Button button in this.buttons )
button.Draw ( batch ); } protected override void updating ( GameTime time )
{ foreach ( Anime anime in this.animes )
anime.Update ( time ); }在 drawing 方法中,我们将绘制按钮和动画以及场景的背景图片。在 updating 方法中,我们将更新场景中的小动画,比如:一只小鸟。
示例
SceneT13 是一个简单的场景,我们将包含两个按钮。
internal sealed class SceneT13
: CommandScene
{ internal SceneT13 ( )
: base ( Vector2.Zero, GestureType.None, "background1",
new Resource[] {
new Resource ( "play.image", ResourceType.Image, @"image\button1" ),
new Resource ( "stop.image", ResourceType.Image, @"image\button2" ),
},
new Making[] {
new Button ( "b.play", "play.image", "PLAY", new Vector2 ( , ), , , new Point ( , ) ),
new Button ( "s.play", "stop.image", "STOP", new Vector2 ( , ), , , new Point ( , ) )
}
)
{ } }在方法 OnNavigatedTo 中,我们将通过 appendScene 方法添加场景 SceneT13。
protected override void OnNavigatedTo ( NavigationEventArgs e )
{
// ... this.appendScene ( new Scene[] { new mygame.test.SceneT13 ( ) } ); base.OnNavigatedTo ( e );
}我们稍微的修改了 appendScene 和 RemoveScene 方法,让他们调用了 sceneAppending 和 sceneRemoving。这样,我们可以通过修改 sceneAppending 和 sceneRemoving 来完成一些工作。
private void appendScene ( Scene scene, Type afterSceneType, bool isInitialized )
{ if ( null == scene )
return; this.sceneAppending ( scene ); // ...
} internal void RemoveScene ( Scene scene )
{ if ( null == scene || !this.scenes.Contains ( scene ) )
return; this.sceneRemoving ( scene ); // ...
}在 sceneAppending 方法中,我们设置了场景 SceneT13 的 Executing 事件。在 sceneRemoving 方法中,我们注销了 Executing 事件。
private void sceneAppending ( Scene scene )
{ if ( scene is mygame.test.SceneT13 )
( scene as mygame.test.SceneT13 ).Executing += this.sceneT13Executing; } private void sceneRemoving ( Scene scene )
{ if ( scene is mygame.test.SceneT13 )
( scene as mygame.test.SceneT13 ).Executing -= this.sceneT13Executing; }在方法 sceneT13Executing 中,我们打印了参数 e 的 Command 字段,他表示被点击按钮的 command。
private void sceneT13Executing ( object sender, SceneEventArgs e )
{
Debug.WriteLine ( "SceneT13: " + e.Command );
}
本期视频 http://v.youku.com/v_show/id_XNTc4NDAwMDEy.html
项目地址 http://wp-xna.googlecode.com/
更多内容 WPXNA
平方开发的游戏 http://zoyobar.lofter.com/
QQ 群 213685539
欢迎访问我在其他位置发布的同一文章:http://www.wpgame.info/post/decc4_71e9c0
使用 CommandScene 类在 XNA 中创建命令场景(十二)的更多相关文章
- 使用 Spirit 类在 XNA 中创建游戏中的基本单位精灵(十三)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 Button 类在 XNA 中创建图形按钮(九)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 Scene 类在 XNA 中创建不同的场景(八)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 Anime 类在 XNA 中创建小动画(十一)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 NPC,NPCManager 在 XNA 中创建 NPC(十九)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
- 使用 NPC,NPCManager 在 XNA 中创建 NPC
使用 NPC,NPCManager 在 XNA 中创建 NPC 平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐 ...
- 【转载】C++ 与“类”有关的注意事项总结(十二):按成员初始化 与 按成员赋值
原文:C++ 与"类"有关的注意事项总结(十二):按成员初始化 与 按成员赋值 一.按成员初始化(与构造函数和拷贝构造函数有关) 用一个类对象初始化另一个类对象,比如: Accou ...
- 编写Java程序,使用ThreadLocal类,项目中创建账户类 Account,类中包括账户名称name、 ThreadLocal 类的引用变量amount,表示存款
查看本章节 查看作业目录 需求说明: 某用户共有两张银行卡,账户名称相同,但卡号和余额不同.模拟用户使用这两张银行卡进行消费的过程,并打印出消费明细 实现思路: 项目中创建账户类 Account,类中 ...
- 使用 Item,ItemManager 在 XNA 中创建物品和道具(十六)
平方已经开发了一些 Windows Phone 上的一些游戏,算不上什么技术大牛.在这里分享一下经验,仅为了和各位朋友交流经验.平方会逐步将自己编写的类上传到托管项目中,没有什么好名字,就叫 WPXN ...
随机推荐
- 菜鸟 学注册机编写之 “MD5”
测试环境 系统: xp sp3 调试器 :od 1.10 sc_office_2003_pro 高手不要见笑,仅供小菜玩乐,有不对或不足的地方还请多多指教,不胜感激! 一:定位关键CALL 1. 因 ...
- uvm_dpi——DPI在UVM中的实现(一)
文件: src/dpi/uvm_dpi.svh 类: 无 SystemVerilog DPI,全称SystemVerilog直接编程接口 (英语:SystemVerilog Direct Pro ...
- SQL优化 · 经典案例 · 索引篇
Introduction 在这些年的工作之中,由于SQL问题导致的数据库故障层出不穷,下面将过去六年工作中遇到的SQL问题总结归类,还原问题原貌,给出分析问题思路和解决问题的方法,帮助用户在使用数据库 ...
- 洛谷 P1951 收费站_NOI导刊2009提高(2)
题目描述 在某个遥远的国家里,有n个城市.编号为1,2,3,…,n. 这个国家的政府修建了m条双向的公路.每条公路连接着两个城市.沿着某条公路,开车从一个城市到另一个城市,需要花费一定的汽油. 开车每 ...
- 将表格table作为execl导出
有时候的需求是从后台获取数据,然后将数据变成execl,进行导出,下载成execl 解决的方法是 一,比较方便的是 这有个插件 可以直接用 https://www.npmjs.com/package/ ...
- 流媒体 8——因特网 tcp/ip
1 因特网 1.1 因特网的结构 组成因特网的子网之间在物理上的相互连接都是通过网关设备实现的.通过网关设备互相连接在一起的不同的网络通常称为子网 (subnetwork),因为它们是大网络之中的网络 ...
- python实现二叉树的镜像
题目描述 操作给定的二叉树,将其变换为源二叉树的镜像. 输入描述: 二叉树的镜像定义:源二叉树 8 / \ 6 10 / \ / \ 5 7 9 11 镜像二叉树 8 / \ 10 6 / \ / \ ...
- 10个HTML5 实战教程 提升你的综合开发能力
HTML5 作为下一代网站开发技术,无论你是一个 Web 开发人员或者想探索新的平台的游戏开发者,都值得去研究.借助尖端功能,技术和 API,HTML5 允许你创建响应性.创新性.互动性以及令人惊叹的 ...
- 标准对象 -------JavaScript
本文摘要:http://www.liaoxuefeng.com/ 在JavaScript的世界里,一切都是对象. 但是某些对象还是和其他对象不太一样.为了区分对象的类型,我们用typeof操作符获取对 ...
- arr.forEach()与for...in的用法举例
1.forEach() 将给定的数字转换成罗马数字. 所有返回的 罗马数字 都应该是大写形式. function convert(num) { var str = ""; var ...