您的位置:首页 > 其它

Windows Phone 7范例游戏Platformer实战8——精灵动画的绘制实现

2010-12-14 10:54 447 查看


Windows Phone 7范例游戏Platformer实战1——5大平台支持
Windows Phone 7范例游戏Platformer实战2——游戏设计初步
Windows Phone 7范例游戏Platformer实战3——游戏资源和内容管道
Windows Phone 7范例游戏Platformer实战4——冲突检测的实现
Windows Phone 7范例游戏Platformer实战5——多点触控编程

Windows Phone 7范例游戏Platformer实战6——加速度传感器解读

Windows Phone 7范例游戏Platformer实战7——简单动画的绘制实现原理

本文参考了木木二进制翻译的Learning XNA 3.0文章,以及CSDN上的 XNA基础一文。非常感谢他们做出的杰出贡献。

在上一小节介绍了宝石的简单动画编程后,现在我们开始学习如何实现一个真正的动画,也就是如何将一连串的图片形成连贯的动作。下面是僵尸怪的精灵图片,我们可以看到它包含10个动作,在XNA中正是将这些动作连贯起来才形成我们看到的僵尸鬼的跑动。本小节我们就是围绕如何将该图片实现为僵尸怪的跑动展开的。

代码

1 #region File Description
2 //-----------------------------------------------------------------------------
3 // AnimationPlayer.cs
4 //
5 // Microsoft XNA Community Game Platform
6 // Copyright (C) Microsoft Corporation. All rights reserved.
7 //-----------------------------------------------------------------------------
8 #endregion
9
10 using System;
11 using Microsoft.Xna.Framework;
12 using Microsoft.Xna.Framework.Graphics;
13
14 namespace Platformer
15 {
16 /// <summary>
17 /// Controls playback of an Animation.
18 /// </summary>
19 struct AnimationPlayer
20 {
21 /// <summary>
22 /// Gets the animation which is currently playing.
23 /// </summary>
24 public Animation Animation
25 {
26 get { return animation; }
27 }
28 Animation animation;
29
30 /// <summary>
31 /// Gets the index of the current frame in the animation.
32 /// </summary>
33 public int FrameIndex
34 {
35 get { return frameIndex; }
36 }
37 int frameIndex;
38
39 /// <summary>
40 /// The amount of time in seconds that the current frame has been shown for.
41 /// </summary>
42 private float time;
43
44 /// <summary>
45 /// Gets a texture origin at the bottom center of each frame.
46 /// </summary>
47 public Vector2 Origin
48 {
49 get { return new Vector2(Animation.FrameWidth / 2.0f, Animation.FrameHeight); }
50 }
51
52 /// <summary>
53 /// Begins or continues playback of an animation.
54 /// </summary>
55 public void PlayAnimation(Animation animation)
56 {
57 // If this animation is already running, do not restart it.
58 if (Animation == animation)
59 return;
60
61 // Start the new animation.
62 this.animation = animation;
63 this.frameIndex = 0;
64 this.time = 0.0f;
65 }
66
67 /// <summary>
68 /// Advances the time position and draws the current frame of the animation.
69 /// </summary>
70 public void Draw(GameTime gameTime, SpriteBatch spriteBatch, Vector2 position, SpriteEffects spriteEffects)
71 {
72 if (Animation == null)
73 throw new NotSupportedException("No animation is currently playing.");
74
75 // Process passing time.
76 time += (float)gameTime.ElapsedGameTime.TotalSeconds;
77 while (time > Animation.FrameTime)
78 {
79 time -= Animation.FrameTime;
80
81 // Advance the frame index; looping or clamping as appropriate.
82 if (Animation.IsLooping)
83 {
84 frameIndex = (frameIndex + 1) % Animation.FrameCount;
85 }
86 else
87 {
88 frameIndex = Math.Min(frameIndex + 1, Animation.FrameCount - 1);
89 }
90 }
91
92 // Calculate the source rectangle of the current frame.
93 Rectangle source = new Rectangle(FrameIndex * Animation.Texture.Height, 0, Animation.Texture.Height, Animation.Texture.Height);
94
95 // Draw the current frame.
96 spriteBatch.Draw(Animation.Texture, position, source, Color.White, 0.0f, Origin, 1.0f, spriteEffects, 0.0f);
97 }
98 }
99 }
100

AnimationPlayer结构体和Animation构成了Platformer游戏的动画基础,所有的和僵尸怪、英雄相关的所有动画实现都是构建于它们之上。完成这两个类后,我们实现僵尸怪的跑动效果就可以说是非常简单了。轩辕将在下一节介绍僵尸怪的跑动动画和它本身的特性的所有实现。

喜欢这篇文章的兄弟们点击文章下面的“推荐”支持下。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐