平方已经开发了一些 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. cocos2d-x-2.2.0_win7+vs2010搭建_eclipse+ndk-r9+cygwin搭建_教程以及编译问题汇总

    声明:我是才用c/c++和cocos2d-x的如果有错误欢迎指出 文章内容我亲测过可以通过,同时我也会一直更新内容 感谢那些把自己的东西分享出来的人 原文地址:http://www.cnblogs.c ...

  2. 数据字典的设计--4.DOM对象的ajax应用

    需求:点击下拉选项框,选择一个数据类型,在表单中自动显示该类型下所有的数据项的名称,即数据库中同一keyword对应的所有不重复的ddlName.      1.在dictionaryIndex.js ...

  3. pc-要实现相隔一定时间数据排序变化一次

    有时候产品会有这种要求,就是展示的数据三天是正序的,一天是逆序的,解决是: 以某一个时间点为基准点,然后获取当前的时间,然后计算差值,分情况 //专利 JPView : function(Sorder ...

  4. PC:各大主板开机启动项快捷键

    组装机主板 品牌笔记本 品牌台式机 主板品牌 启动按键 笔记本品牌 启动按键 台式机品牌 启动按键 华硕主板 F8 联想笔记本 F12 联想台式机 F12 技嘉主板 F12 宏基笔记本 F12 惠普台 ...

  5. 通过cmd查看环境变量名对应的环境变量值

    在VS环境中通常要添加路径,不过基本都是按照往上提供的方法添加变量名形如:$(VC_IncludePath),但是如何通过cmd命令找到真正的路径呢 未完待续……

  6. JZTK项目 驾照题库项目servlet层得到的json字符串在浏览器中 汉字部分出现问号?无法正常显示的解决方法

    servlet层中的代码如下: package com.swift.jztk.servlet; import java.io.IOException; import javax.servlet.Ser ...

  7. 【数学 随机 技巧】cf364D. Ghd

    随机化选讲的例题 John Doe offered his sister Jane Doe find the gcd of some set of numbers a. Gcd is a positi ...

  8. js浮点数加减乘除

    浮点数精确计算 /** ** 加法函数,用来得到精确的加法结果 ** 说明:javascript的加法结果会有误差,在两个浮点数相加的时候会比较明显.这个函数返回较为精确的加法结果. ** 调用:ac ...

  9. day 44 前端HTML

    前端HTML   HTML介绍 Web服务本质 import socket sk = socket.socket() sk.bind(("127.0.0.1", 8080)) sk ...

  10. Insertion Sort Gym - 101955C 思路+推公式

    题目:题目链接 题意:对长为n的1到n的数列的前k个数排序后数列的最长上升子序列长度不小于n-1的数列的种数,训练赛时怎么都读不明白这个题意,最后还是赛后问了旁队才算看懂,英语水平急需拯救55555 ...