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

Anime

Anime 类继承自 Movie 类,唯一的区别在于,Anime 类将包含一些动作,这些动作将说明 Anime 的运动方式,你可以使用 Anime 类来表示页面上移动的白云。Anime 的字段 actions 表示所有动作,其中 AnimeAction 是一个基类,之后我们会说明他。

private readonly List<AnimeAction> actions = new List<AnimeAction> ( );

internal Anime ( string name, string resourceName, Vector2 location, int width, int height, int rate, string defaultSequenceName, MovieSequence[] sequences, params AnimeAction[] actions )
: base ( name, resourceName, location, width, height, rate,
0f,
defaultSequenceName, sequences )
{ if ( null != actions )
foreach ( AnimeAction action in actions )
if ( null != action )
{
action.Anime = this;
this.actions.Add ( action );
} }

在 Anime 的构造函数中,我们将参数 actions 中的 AnimeAction 保存到字段 actions 中。而其余的参数和 Movie 所使用的参数类似,这里不再说明。

internal void Update ( GameTime time )
{ foreach ( AnimeAction action in this.actions )
action.Update ( time ); Movie.NextFrame ( this );
}

在 Anime 的 Update 方法中,我们将调用每一个 AnimeAction 的 Update 方法,这样 AnimeAction 就能够控制 Anime 的状态,比如:位置,速度等。此外,我们还需要调用 Movie 类的 NextFrame 方法,这样动画才能被播放。

internal static void Draw ( Anime anime, GameTime time, SpriteBatch batch )
{ Movie.Draw ( anime, time, batch ); }

在 Anime 的 Draw 方法中,我们直接简单的调用 Movie 的 Draw 方法来绘制动画。

AnimeAction

AnimeAction 是所有动画动作的基类,他本身很简单,代码如下:

internal abstract class AnimeAction
{
internal Anime Anime; protected AnimeAction ( )
{ } internal abstract void Update ( GameTime time ); }

字段 Anime 被用来控制 Anime,比如:移动 Anime 的位置。他是在 Anime 的构造函数中被设置的,而 AnimeAction 类并不会修改它。

所有从 Anime 继承的类都必须实现方法 Update,在这个方法中,我们将使用字段 Anime 来调整 Anime 的状态。

AnimeMovementAction

AnimeMovementAction 是一个继承自 AnimeAction 的类,用来控制 Anime 的位置,他可以实现 Anime 的左右移动,下面是 AnimeMovementAction 中的字段。

private readonly Rectangle area;

private float xSpeed;
private float ySpeed; private readonly long xTurnFrameCount;
private readonly long yTurnFrameCount; private long xTurnFrameIndex;
private long yTurnFrameIndex;

字段 area 表示一个区域,用来限制 Anime 的移动,比如:如果 Anime 移动到了区域的右边,则他将在区域的左边出现。

字段 xSpeed,ySpeed 表示 Anime 在 x,y 轴上移动速度。字段 xTurnFrameCount,yTurnFrameCount,xTurnFrameIndex,yTurnFrameIndex 用来记录在合适 Anime 的速度将被反转。

在 AnimeMovementAction 的构造函数中,我们设置了这些字段,其中还使用了 World 的 ToFrameCount 函数。在 Update 方法中,我们根据字段设置了 Anime 的位置。

internal AnimeMovementAction ( float xSpeed, float ySpeed, float xTurnSecond, float yTurnSecond, float xCurrentSecond, float yCurrentSecond, Rectangle area )
: base ( )
{
this.xTurnFrameCount = World.ToFrameCount ( xTurnSecond );
this.yTurnFrameCount = World.ToFrameCount ( yTurnSecond );
this.xSpeed = xSpeed;
this.ySpeed = ySpeed; this.xTurnFrameIndex = World.ToFrameCount ( xCurrentSecond );
this.yTurnFrameIndex = World.ToFrameCount ( yCurrentSecond ); this.area = area;
} internal override void Update ( GameTime time )
{ if ( this.xTurnFrameCount > && this.xTurnFrameIndex++ > this.xTurnFrameCount )
{
this.xTurnFrameIndex = ;
this.xSpeed = -this.xSpeed;
} if ( this.yTurnFrameCount > && this.yTurnFrameIndex++ > this.yTurnFrameCount )
{
this.yTurnFrameIndex = ;
this.ySpeed = -this.ySpeed;
} this.Anime.Location += new Vector2 ( this.xSpeed, this.ySpeed ); if ( !this.area.IsEmpty )
{
Vector2 location = this.Anime.Location; if ( this.xSpeed > )
{ if ( location.X - this.Anime.Width > this.area.Right )
this.Anime.Location = new Vector2 ( this.area.Left, location.Y ); }
else if ( this.xSpeed < )
if ( location.X < this.area.Left )
this.Anime.Location = new Vector2 ( this.area.Right + this.Anime.Width, location.Y ); if ( this.ySpeed > )
{ if ( location.Y > this.area.Bottom )
this.Anime.Location = new Vector2 ( location.X, this.area.Top - this.Anime.Height ); }
else if ( this.ySpeed < )
if ( location.Y + this.Anime.Height < this.area.Top )
this.Anime.Location = new Vector2 ( location.X, this.area.Bottom ); } }

ToFrameCount,FrameRate

World 类的 FrameRate 属性表示在游戏中每一秒的帧数,方法 ToFrameCount 可以返回指定时间内执行的帧数。

private static int frameRate = ;
internal static int FrameRate
{
get { return frameRate; }
set { frameRate = value <= ? : value; }
} internal static long ToFrameCount ( double second )
{ return ( long ) ( second * World.FrameRate ); }
internal static long ToFrameCount ( float second )
{ return ( long ) ( second * World.FrameRate ); }

示例

场景 SceneT12 包含两个小鸟的动画,bird1 可以从左到右飞行并不断重复,bird2 可以垂直移动。

在 drawing 方法中,我们需要调用 Anime 的 Draw 方法来绘制小鸟。而在 updating 方法中,我们需要调用 Anime 的 Update 方法来更新位置。

internal sealed class SceneT12
: Scene
{
private readonly Anime bird1;
private readonly Anime bird2; internal SceneT12 ( )
: base ( Vector2.Zero, GestureType.None,
new Resource[] {
new Resource ( "bird2.image", ResourceType.Image, @"image\bird2" ),
},
new Making[] {
new Anime ( "b1", "bird2.image", new Vector2 ( , ), , , , "a",
new MovieSequence[] { new MovieSequence ( "a", true, new Point ( , ), new Point ( , ) ) },
new AnimeMovementAction ( , , new Rectangle ( -, , , ) )
),
new Anime ( "b2", "bird2.image", new Vector2 ( , ), , , , "a",
new MovieSequence[] { new MovieSequence ( "a", true, new Point ( , ), new Point ( , ) ) },
new AnimeMovementAction ( , , , )
), }
)
{
this.bird1 = this.makings[ "b1" ] as Anime;
this.bird2 = this.makings[ "b2" ] as Anime;
} protected override void drawing ( GameTime time, SpriteBatch batch )
{
base.drawing ( time, batch ); Anime.Draw ( this.bird1, time, batch );
Anime.Draw ( this.bird2, time, batch );
} protected override void updating ( GameTime time )
{
this.bird1.Update ( time );
this.bird2.Update ( time ); base.updating ( time );
} }

在 World 的 OnNavigatedTo 方法中,我们添加了新的 SceneT12。

protected override void OnNavigatedTo ( NavigationEventArgs e )
{
// ... this.appendScene ( new Scene[] { new mygame.test.SceneT12 ( ) } ); base.OnNavigatedTo ( e );
}

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

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

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

QQ 群 213685539

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

使用 Anime 类在 XNA 中创建小动画(十一)的更多相关文章

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

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

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

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

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

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

  4. 使用 Scene 类在 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. Mac 颜色取值

    command+shift+4  截图,我靠,我以前不知道 系统自带数码测色计, 选择显示十六位制 command+L 锁定 command+shift+c 复制 简直太方便

  2. 【Quartus错误】Internal Error: Sub-system: AMERGE

    错误内容:Internal Error: Sub-system: AMERGE, File: /quartus/atm/amerge/amerge_kpt_op.cpp, Line: 220 解决方案 ...

  3. Dll注入:X86/X64 远程线程CreateRemoteThread 注入

    远线程注入原理是利用Windows 系统中CreateRemoteThread()这个API,其中第4个参数是准备运行的线程,我们可以将LoadLibrary()填入其中,这样就可以执行远程进程中的L ...

  4. 流媒体 8——因特网 tcp/ip

    1 因特网 1.1 因特网的结构 组成因特网的子网之间在物理上的相互连接都是通过网关设备实现的.通过网关设备互相连接在一起的不同的网络通常称为子网 (subnetwork),因为它们是大网络之中的网络 ...

  5. ABAP和Hybris的源代码生成工具

    ABAP 有两种方式,一种是ABAP Code Composer, 细节可以查看我的博客Step by Step to generate ABAP code automatically using C ...

  6. 【BZOJ4540】 [HNOI2016] 序列(莫队)

    点此看题面 大致题意: 求出一个序列的一段区间中所有子序列最小值之和. 莫队 这道题其实是一道莫队题. 但是需要大量的预处理. 预处理 先考虑预处理两个数组\(lst_i\)和\(nxt_i\),分别 ...

  7. 第十六篇、OC_按比例适配

    // 屏幕高度 #define XMGHeight [UIScreen mainScreen].bounds.size.height // 屏幕宽度 #define XMGWidth [UIScree ...

  8. advanced regression to predict housing prices

    https://docs.google.com/presentation/d/e/2PACX-1vQGlXP6QZH0ATzXYwnrXinJcCn00fxCOoEczPAXU-n3hAPLUfMfi ...

  9. vue入门之vue-cli安装项目

    第一步先安装nmp 在node.js的官网下载即可. 第二步 直接安装刚下载好的node.js即可,(这里建议不要修改node.js的安装路径),傻瓜式直接下一步即可 检测是否安装成功: 在cmd的控 ...

  10. SVG path

    在网页上画一图形,比如星星或波浪线,开始是想着图形软件画一个的,后来发现SVG这绘图程序的语言,感觉甚是可以,就发了些时间学了一下,在此做一简单分享和记录. 菜鸟上是这么介绍的(SVG 是使用 XML ...