平方已经开发了一些 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. jsop解析获得htmldome

    package com.open1111.jsoup; import org.apache.http.HttpEntity;import org.apache.http.client.methods. ...

  2. ImportError: No module named PIL

    解决方法: 在C:\Python27\Scripts目录下打开命令行,输入: pip install Pillow

  3. 比特币中P2SH(pay-to-script-hash)多重签名的锁定脚本和解锁脚本

    P2SH(pay-to-script-hash)多重签名的脚本 P2SH是多重签名的一种应用形式.在P2SH的交易中,多了一个Redeem Script的概念,称为赎回脚本.当向P2SH脚本的地址转账 ...

  4. Extjs4.1+desktop+SSH2 定义程序入口

    app.js定义程序入口: MainController.js: 加载控制器: 外部组件引用入口loader.js 时间组件 静态变量组件: 引入comm.js index.jsp 验证打印 comm ...

  5. warning: remote HEAD refers to nonexistent ref, unable to checkout.解决

    git branch -r origin/branch origin/hexo git checkout -b hexo origin/hexo

  6. 进入Windows之前发出警告

    实现效果: 知识运用: 通过注册表中HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Winlogon\子键下的LegalNoticeCaption ...

  7. Angular 2 树节点的上下移动问题

    最近在做一个树节点的上下移动然后实现排序的问题.直接看图: 实现已选查询条件的上下移动.结合了primeng 的picklist 组件. 下面是html代码 <p-tabPanel header ...

  8. Express session的使用

    进行session存储时需引用中间件,app.js var express=require('express'); var app=express(); var cookieParser = requ ...

  9. Bzoj 1081 [Ahoi2009] chess 中国象棋

    bzoj 1081 [Ahoi2009] chess 中国象棋 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1801 状态比较难设,的确 ...

  10. spring data事务

    事务在spring data中的使用 1:事务一般在service层.因为一个service方法可能会多次调用不同的dao,为了保证事务的完整性,那么多次的dao都放到一个方法里面 2:读的时候可以不 ...