平方已经开发了一些 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. 条件注解@Conditional

    通过活动的profile,可以获得不同的Bean.Spring4提供了一个更通用的基于条件的Bean的创建,即使用@Conditonal注解 @Conditional根据满足某一个特定条件创建一个特定 ...

  2. ubuntu16.04解决屏幕适应问题

    打开ubuntu登录进去后,输入: sudo  apt-get installopen-vm-tools sudo apt-get install open-vm* 然后重启(reboot),即可解决 ...

  3. hihocoder 第四十周 三分求极值

    题目链接:http://hihocoder.com/contest/hiho40/problem/1 ,一道简单的三分. 题目是在直角坐标系中有一条抛物线y=ax^2+bx+c和一个点P(x,y),求 ...

  4. 配置Maven镜像与本地缓存

    IntelliJ IDEA 安装后自带Maven,也可以使用自己安装的Maven. 配置阿里镜像与本地仓库文件夹 找到Maven的安装目录 打开settings.xml配置文件   修改mirrors ...

  5. iphone 弹出键盘,文本框自动向上移动。

    1.让类继承UITextViewDelegate UITextView *inputTextView;UIScrollView * _scrollView; 2.在init函数中先创建scrollVi ...

  6. PS 厘米与像素切换

    方法一: 快捷键 ctrl + r   打开标尺将鼠标放在标尺刻度上右键 出现菜单里修改即可: 方法二: 编辑---首选项---单位与标尺 修改即可:

  7. 访问虚拟机中web服务的

    经常发现假如我们想弄一点小玩意或跑一些小demo,总是要不断的在自己的工作本本上搭建不同的运行环境,久而久之,本本上充斥着各种软件,速度下降了,同时管理也非常的不方便.于是想到用虚拟机来搭建运行环境, ...

  8. 高阶函数 -------JavaScript

    高阶函数 本文摘要:http://www.liaoxuefeng.com/ JavaScript的函数其实都指向某个变量.既然变量可以指向函数,函数的参数能接收变量,那么一个函数就可以接收另一个函数作 ...

  9. 如何修改魔兽争霸war3分辨率

    如何修改魔兽争霸war3 分辨率 有时候发现老电脑从XP系统升级到WIN7之后,发现玩魔兽不能全屏了(2边会有一些黑屏的).最后检查发现是魔兽在安装注册表之后显示的分辨率跟电脑的实际分辨率不同导致的. ...

  10. java算法面试题:递归算法题1

    递归算法题1 一个整数,大于0,不用循环和本地变量,按照n,2n,4n,8n的顺序递增,当值大于5000时,把值按照指定顺序输出来.例:n=1237则输出为:1237,2474,4948,9896,9 ...