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

场景,屏幕

这里的场景也就是屏幕或者页面,比如我们常说的主屏幕,主屏幕上通常有一个开始的按钮。平方创建了 Scene 类来表示一个屏幕,页面,场景。而 Scene 类中将包含我们之前所将到的一些类,比如:ResourceManager,AudioManager 等。

下面中 Scene 类的一些字段和属性。

internal readonly GestureType GestureType;
private bool isClosed;
public bool IsClosed
{
get { return this.isClosed; }
set { this.isClosed = value; }
}
protected bool isEnabled = true;
public bool IsEnabled
{
get { return this.isEnabled; }
} internal readonly bool IsBroken; internal readonly Vector2 Location;
protected World world;
public World World
{
get { return this.world; }
set
{
this.resourceManager.World = value;
this.world = value;
}
} private SpriteBatch spiritBatch;
private readonly ResourceManager resourceManager;
protected AudioManager audioManager;
public AudioManager AudioManager
{
get { return this.audioManager; }
}
private readonly bool isBackgroundMusicLoop; protected readonly Dictionary<string, Making> makings = new Dictionary<string, Making> ( );
public Dictionary<string, Making> Makings
{
get { return this.makings; }
}

字段 GestureType 表示 Scene 所支持的手势,比如:双击,单击等。

属性 IsClosed 表示 Scene 是否已经关闭,而属性 IsEnabled 表示 Scene 是否可用。

字段 IsBroken 表示是否进行精灵的绘制。字段 Location 表示 Scene 的位置。属性 World 表示场景所在的 World。

字段 spiritBatch 用来绘制屏幕中所有的内容,他从 World 中获得。字段 resourceManager 用来管理所有需要的资源,字段 audioManager 用来管理音频,字段 isBackgroundMusicLoop 表示背景音乐是否可以循环播放。

属性 Makings 表示屏幕中所有的元件,比如:标签。

之后,我们在构造函数中初始化一些字段。

protected Scene ( Vector2 location, GestureType gestureType, IList<Resource> resources, IList<Making> makings, bool isBroken, bool isBackgroundMusicLoop )
{
this.Location = location;
this.GestureType = gestureType; this.resourceManager = new ResourceManager ( resources );
this.audioManager = new AudioManager ( ); if ( null != makings )
foreach ( Making making in makings )
if ( null != making )
this.makings.Add ( making.Name, making ); foreach ( Making making in this.makings.Values )
making.Init ( this ); this.IsBroken = isBroken;
this.isBackgroundMusicLoop = isBackgroundMusicLoop;
}

注意这里,我们为 Making 类增加了一个 Init 方法,用来初始化元件。我们看到,在 Making 的 Init 方法中,如果 Making 实现了 ILockable 接口,则我们根据 Scene 的位置来修改 Making 的位置。

protected Scene scene;

internal virtual void Init ( Scene scene )
{
this.scene = scene; if ( this is ILockable )
( this as ILockable ).Location += scene.Location; }

在 LoadContent 方法中,我们从 World 获取了 SpriteBatch 服务,并调用了 ResourceManager 的 LoadContent 方法载入资源,并将这些资源传递给 Making 和 AudioManager。此后,AudioManager 试图播放名称为 scene.sound 的音乐。

在 UnloadContent 方法中,我们卸载 ResourceManager 所加载的资源。而在 Dispose 中,除了卸载资源,我们还会调用 Making 的 Dispose 方法。

public virtual void LoadContent ( )
{ this.spiritBatch = this.world.Services.GetService ( typeof ( SpriteBatch ) ) as SpriteBatch; this.resourceManager.LoadContent ( ); foreach ( Making making in this.makings.Values )
making.InitResource ( this.resourceManager ); this.audioManager.LoadContent ( this.resourceManager ); this.audioManager.PlayMusic ( "scene.sound", this.isBackgroundMusicLoop );
} public virtual void UnloadContent ( )
{
this.audioManager.UnloadContent ( ); this.resourceManager.UnloadContent ( );
} public virtual void Dispose ( )
{ foreach ( Making making in this.makings.Values )
making.Dispose ( ); this.UnloadContent ( );
}

在 Update 方法中,我们判断如果 Scene 被关闭或者不可用,则不进行更新,从 Scene 派生的类可以修改 updating 方法来实现自己的功能。而 Draw 方法中,我们判断如果 Scene 被关闭,则不进行绘制,从 Scene 派生的类可以修改 drawing 方法来绘制自己的内容。

protected virtual void updating ( GameTime time )
{ } public void Update ( GameTime time )
{ if ( this.isClosed || !this.isEnabled )
return; this.updating ( time );
} protected virtual void drawing ( GameTime time, SpriteBatch batch )
{ } public void Draw ( GameTime time )
{ if ( this.isClosed )
return; this.spiritBatch.Begin ( );
this.drawing ( time, this.spiritBatch );
this.spiritBatch.End ( );
}

Scene 类还接受 Controller 类,用来判断用户的输入。派生类可以修改 inputing 方法来完成相关的操作。

protected virtual void inputing ( Controller controller )
{ } public bool Input ( Controller controller )
{ if ( this.isClosed || !this.isEnabled )
return false; this.inputing ( controller );
return true;
}

最后,Scene 类可以调用自己的 Close 方法来关闭自己。在关闭之前,我们还触发了 Closing 和 Closed 事件,可以在此时弹出窗口询问用户是否关闭。

internal event EventHandler<SceneEventArgs> Closing;
internal event EventHandler<SceneEventArgs> Closed; public void Close ( )
{ if ( this.isClosed )
return; if ( null == this.world )
throw new NullReferenceException ( "world can't be null when closing scene" ); if ( null != this.Closing )
{
SceneEventArgs closingArg = new SceneEventArgs ( ); this.Closing ( this, closingArg ); if ( closingArg.IsCancel )
return; } if ( null != this.Closed )
this.Closed ( this, new SceneEventArgs ( ) ); this.world.RemoveScene ( this );
}

修改 World 类

为 World 增加一个 isPreserved 用来表示游戏是否直接从内存恢复,我们可以在 activate 方法中,通过 IsApplicationInstancePreserved 属性来获取他,如果 isPreserved 为 true,则我们不需要再次执行 OnNavigatedTo 中的代码。

private bool isPreserved = false;

private void activate ( object sender, ActivatedEventArgs e )
{ this.isPreserved = e.IsApplicationInstancePreserved; } protected override void OnNavigatedTo ( NavigationEventArgs e )
{ if ( this.isPreserved )
return; // ...
}

增加一个 isInitialized 字段,用来表示 World 是否已经初始化,以此来决定是否初始化被 World 管理的 Scene。在方法 OnNavigatedTo 中,我们会设置 isInitialized 为 true。

private bool isInitialized = false;

protected override void OnNavigatedTo ( NavigationEventArgs e )
{
// ... this.isInitialized = true; // ...
}

之后,我们要为 World 增加管理 Scene 的功能。

private readonly List<Scene> scenes = new List<Scene> ( );

protected override void OnNavigatedTo ( NavigationEventArgs e )
{
// ... foreach ( Scene scene in this.scenes )
scene.LoadContent ( ); // ...
} private void appendScene ( Scene scene, Type afterSceneType, bool isInitialized )
{ if ( null == scene )
return; if ( !isInitialized )
{
scene.World = this;
scene.IsClosed = false; if ( this.isInitialized )
scene.LoadContent ( ); } int index = this.getSceneIndex ( afterSceneType ); if ( index < )
this.scenes.Add ( scene );
else
this.scenes.Insert ( index, scene ); TouchPanel.EnabledGestures = scene.GestureType;
} internal void RemoveScene ( Scene scene )
{ if ( null == scene || !this.scenes.Contains ( scene ) )
return; scene.IsClosed = true; if ( this.isInitialized )
try
{ if ( null != scene )
scene.Dispose ( ); }
catch
{ scene.Dispose ( ); } this.scenes.Remove ( scene ); if ( this.scenes.Count > )
TouchPanel.EnabledGestures = this.scenes[ this.scenes.Count - ].GestureType; } private int getSceneIndex ( Type sceneType )
{ if ( null == sceneType )
return -; string type = sceneType.ToString ( ); for ( int index = this.scenes.Count - ; index >= ; index-- )
if ( this.scenes[ index ].GetType ( ).ToString ( ) == type )
return index; return -;
} private T getScene<T> ( )
where T : Scene
{
int index = this.getSceneIndex ( typeof ( T ) ); return index == - ? null : this.scenes[ index ] as T;
}

字段 scenes 中包含了 World 所管理的所有场景,在 OnNavigatedTo 方法中,我们会对已经添加的场景调用一次 LoadContent 方法,但这种情况较少出现。

方法 appendScene 用于将场景添加到 World 当中,参数 afterSceneType 用来指示场景的次序,参数 isInitialized 一般为 false,表示需要初始化场景的资源。

方法 RemoveScene 用于将场景从 World 中移除,getSceneIndex 方法用来获取某一个场景的索引,方法 getScene<T7gt; 用来获取指定的场景。

private void OnUpdate ( object sender, GameTimerEventArgs e )
{ if ( !this.IsEnabled )
return; this.controller.Update ( ); Scene[] scenes = this.scenes.ToArray ( );
GameTime time = new GameTime ( e.TotalTime, e.ElapsedTime ); foreach ( Scene scene in scenes )
if ( null != scene )
scene.Update ( time ); for ( int index = scenes.Length - ; index >= ; index-- )
if ( null != scenes[ index ] && scenes[ index ].Input ( this.controller ) )
break; } private void OnDraw ( object sender, GameTimerEventArgs e )
{
this.GraphicsDevice.Clear ( this.BackgroundColor ); bool isBroken = false;
GameTime time = new GameTime ( e.TotalTime, e.ElapsedTime ); foreach ( Scene scene in this.scenes.ToArray ( ) )
if ( null != scene )
{ if ( !isBroken && scene.IsBroken )
{
// Draw sprites
isBroken = true;
} scene.Draw ( time );
} if ( !isBroken )
// Draw sprites.
; }

在 OnUpdate 方法中,我们会更新每一个场景,而只有第一个场景可以接受用户的输入。在 OnDraw 方法中,我们会逐个绘制场景。

一个例子

我们创建一个名为 SceneT9 的场景。

internal sealed class SceneT9
: Scene
{
private readonly Label l1;
private readonly Movie bird2; internal SceneT9 ( )
: base ( Vector2.Zero, GestureType.Tap | GestureType.DoubleTap,
new Resource[] {
new Resource ( "bird2.image", ResourceType.Image, @"image\bird2" ),
new Resource ( "click.s", ResourceType.Sound, @"sound\click" ),
new Resource ( "peg", ResourceType.Font, @"font\myfont" ),
},
new Making[] {
new Movie ( "bird2.m", "bird2.image", new Vector2 ( , ), , , , , "live",
new MovieSequence ( "live", true, new Point ( , ), new Point ( , ) ),
new MovieSequence ( "dead", , false, new Point ( , ), new Point ( , ) )
),
new Label ( "l1", "Hello windows phone!", 2f, Color.LightGreen, 0f )
}
)
{
this.l1 = this.makings[ "l1" ] as Label;
this.bird2 = this.makings[ "bird2.m" ] as Movie;
} protected override void inputing ( Controller controller )
{ if ( !controller.IsGestureEmpty && controller.Gestures[ ].GestureType == GestureType.Tap )
this.audioManager.PlaySound ( "click.s" ); } protected override void updating ( GameTime time )
{
Movie.NextFrame ( this.bird2 ); base.updating ( time );
} protected override void drawing ( GameTime time, SpriteBatch batch )
{
base.drawing ( time, batch ); Label.Draw ( this.l1, batch );
Movie.Draw ( this.bird2, time, batch );
} }

在 SceneT9 中,我们为场景添加了所需要的资源,和两个元件,一个是小鸟电影,一个是标签。通过场景的 makings 字段我们将这个两个元件分别保存在 l1 和 bird2 中。

在 updating 和 drawing 方法中,我们分别更新了电影和绘制元件。在 inputing 方法中,我们判断用户是否有点击的动作并播放点击的声音。

在 World 的 OnNavigatedTo 方法中,我们使用 appendScene 方法来添加 SceneT9。

protected override void OnNavigatedTo ( NavigationEventArgs e )
{
// ... this.appendScene ( new mygame.test.SceneT9 ( ), null, false ); base.OnNavigatedTo ( e );
}

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

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

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

QQ 群 213685539

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

使用 Scene 类在 XNA 中创建不同的场景(八)的更多相关文章

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

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

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

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

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

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

  4. 使用 Anime 类在 XNA 中创建小动画(十一)

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

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

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

  6. 编写Java程序,使用ThreadLocal类,项目中创建账户类 Account,类中包括账户名称name、 ThreadLocal 类的引用变量amount,表示存款

    查看本章节 查看作业目录 需求说明: 某用户共有两张银行卡,账户名称相同,但卡号和余额不同.模拟用户使用这两张银行卡进行消费的过程,并打印出消费明细 实现思路: 项目中创建账户类 Account,类中 ...

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

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

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

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

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

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

随机推荐

  1. IDEA安装及基本配置

    IDEA基本介绍 IntelliJ IDEA是JetBrains公司开发的一款开发Java的开发工具,简称IDEA,JetBrains公司还有其他几款优秀的开发工具. IDEA是一款收费软件,在财力允 ...

  2. SVN合并步骤

    1.trunk->branch/tag 分支路径在分支文件夹中,选择右键检出 2.合并分支到主干分支新增 1.txt 文件 需要合并到主干 在trunck->鼠标右键合并->合并到不 ...

  3. python零基础学习开篇

    我是某工业大学的一名博士,研究方向是基于人脸的模式识别.很遗憾,毕业后没有继续从事图像处理中模式识别相关研究.多种原因进入了一家国企单位,从事交通方面工作.工作2年中一直迷茫,没有在工作岗位中找到自己 ...

  4. 前端安全系列(二):如何防止CSRF攻击?

    前端安全系列(二):如何防止CSRF攻击?   背景 随着互联网的高速发展,信息安全问题已经成为企业最为关注的焦点之一,而前端又是引发企业安全问题的高危据点.在移动互联网时代,前端人员除了传统的 XS ...

  5. 剑指offer22 栈的压入、弹出序列

    写的一个代码,虽然正确通过了,但我觉得会报vector越界的错误 class Solution { public: bool IsPopOrder(vector<int> pushV,ve ...

  6. 解决sublime text 2总是在新窗口中打开文件(标签中打开)

    在mac下不是很喜欢sublime text 2 总是在新窗口中打开文件,很麻烦,文件打多了,就会出现N多窗口,虽然可以直接打开当前目录可以解决,但有时候查看其它项目中的单个文件,就比较麻烦.百度一直 ...

  7. C10 C语言数据结构

    目录 枚举 结构体 共用体 枚举 enum enum枚举是 C 语言中的一种基本数据类型,它可以让数据更简洁,更易读. 枚举语法定义格式为: enum 枚举名 {枚举元素1,枚举元素2,……}; 枚举 ...

  8. ovx openVirtex安装

    搞了好久的pox实验,中途一个星期没更新了吧, 今天继续... 新手第一次搞还是在虚拟机上最安全~ ovx参照上面的做吧,注意必须在联网的情况下,否则很多控件都连不上(第一次做的时候虚拟机没配ip 坑 ...

  9. JQuery EasyUI学习记录(一)

    1.主页设计(JQuery EasyUI插件) 下载easyUI开发包: 将easyUI资源文件导入页面中: <link rel="stylesheet" type=&quo ...

  10. skynet 学习笔记-netpack模块(1)

    int luaopen_netpack(lua_State *L) { luaL_checkversion(L); luaL_Reg l[] = { { "pop", lpop } ...