XNA4.0学习指南第五章代码中文注释(Sprite精灵基类)

1 using System;
2  using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using Microsoft.Xna.Framework;
6 using Microsoft.Xna.Framework.Graphics;
7
8 namespace AnimatedSprites
9 {
10 /// <summary>
11 /// 抽象的精灵基类
12 /// </summary>
13 abstract class Sprite
14 {
15 #region ■变量声明______________________________________________________________
16
17 /// <summary>
18 /// 要绘制的精灵或精灵位图
19 /// </summary>
20 Texture2D textureImage;
21 /// <summary>
22 /// 精灵位图中单帧的尺寸
23 /// </summary>
24 protected Point frameSize;
25 /// <summary>
26 /// 当前帧在精灵位图中的索引
27 /// </summary>
28 Point currentFrame;
29 /// <summary>
30 /// 精灵位图中的行/列数
31 /// </summary>
32 Point sheetSize;
33 /// <summary>
34 /// 偏移量用来修改精灵碰撞检测中用到的包围矩形
35 /// </summary>
36 int collisionOffset;
37 /// <summary>
38 /// 自上一帧到当前帧经过的时间(毫秒数),这里初始化为0
39 /// </summary>
40 int timeSinceLastFrame = 0;
41 /// <summary>
42 /// 帧间等待的时间(毫秒数)
43 /// </summary>
44 int millisecondsPerFrame;
45 /// <summary>
46 /// 默认的帧间等待的时间(毫秒数,即原来的60fps)
47 /// </summary>
48 const int defaultMillisecondsPerFrame = 16;
49 /// <summary>
50 /// 精灵来在X,Y方向移动的速度
51 /// </summary>
52 protected Vector2 speed;
53 /// <summary>
54 /// 精灵被绘制的位置
55 /// </summary>
56 protected Vector2 position;
57
58 #endregion
59
60 #region ■属性声明______________________________________________________________
61
62 /// <summary>
63 /// 用来让精灵往这个方向移动的属性,抽象属性,让不同子类来实现不同的方向控制
64 /// </summary>
65 public abstract Vector2 direction { get; }
66 /// <summary>
67 /// 用来进行碰撞检测的矩形值的属性
68 /// </summary>
69 public Rectangle collisionRect
70 {
71 get
72 {
73 return new Rectangle((int)position.X + collisionOffset, (int)position.Y + collisionOffset, frameSize.X - (collisionOffset * 2), frameSize.Y - (collisionOffset * 2));
74 }
75 }
76
77 #endregion
78
79 #region ■构造方法______________________________________________________________
80
81 // 两个构造方法唯一不同的地方是第二个方法需要一个用来计算动画速度的变量millisecondsPerFrame
82
83 /// <summary>
84 /// 构造方法
85 /// </summary>
86 /// <param name="textureImage">要绘制的精灵或精灵位图</param>
87 /// <param name="position">精灵被绘制的位置</param>
88 /// <param name="frameSize">精灵位图中单帧的尺寸</param>
89 /// <param name="collisionOffset">偏移量用来修改精灵碰撞检测中用到的包围矩形</param>
90 /// <param name="currentFrame">偏移量用来修改精灵碰撞检测中用到的包围矩形</param>
91 /// <param name="sheetSize">精灵位图中的行/列数</param>
92 /// <param name="speed">精灵来在X,Y方向移动的速度</param>
93 public Sprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed)
94 : this(textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, speed, defaultMillisecondsPerFrame)
95 {
96 }
97
98 /// <summary>
99 /// 构造方法
100 /// </summary>
101 /// <param name="textureImage">要绘制的精灵或精灵位图</param>
102 /// <param name="position">精灵被绘制的位置</param>
103 /// <param name="frameSize">精灵位图中单帧的尺寸</param>
104 /// <param name="collisionOffset">偏移量用来修改精灵碰撞检测中用到的包围矩形</param>
105 /// <param name="currentFrame">偏移量用来修改精灵碰撞检测中用到的包围矩形</param>
106 /// <param name="sheetSize">精灵位图中的行/列数</param>
107 /// <param name="speed">精灵来在X,Y方向移动的速度</param>
108 /// <param name="millisecondsPerFrame">帧间等待的时间(毫秒数)</param>
109 public Sprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed, int millisecondsPerFrame)
110 {
111 this.textureImage = textureImage;
112 this.position = position;
113 this.frameSize = frameSize;
114 this.collisionOffset = collisionOffset;
115 this.sheetSize = sheetSize;
116 this.speed = speed;
117 this.millisecondsPerFrame = millisecondsPerFrame;
118 }
119
120 #endregion
121
122 #region ■成员方法______________________________________________________________
123
124 /// <summary>
125 /// 精灵的更新方法
126 /// </summary>
127 /// <param name="gameTime">GameTime</param>
128 /// <param name="clientBounds">代表游戏窗口客户区矩形,用来检测物体何时越过了游戏窗口边缘</param>
129 public virtual void Update(GameTime gameTime, Rectangle clientBounds)
130 {
131 // 改变精灵位图
132 // gameTime.ElapsedGameTime属性表示上一次调用Update方法后经过的时间
133 // +=上一次更新后经过的时间,如果大于指定的时间millisecondsPerFrame,则执行代码
134 timeSinceLastFrame += gameTime.ElapsedGameTime.Milliseconds;
135 if (timeSinceLastFrame > millisecondsPerFrame)
136 {
137 // 重置为0,下一次执行更新再用来判断
138 timeSinceLastFrame = 0;
139 // currentFrame.X先自加1,如果大于或等于sheetSize.X,即如果到了最右边的位图
140 if (++currentFrame.X >= sheetSize.X)
141 {
142 // 将currentFrame.X置零,回到第一列的位图
143 currentFrame.X = 0;
144 // currentFrame.Y先自加1,如果大于或等于sheetSize.Y,即如果到了最下边的位图
145 if (++currentFrame.Y >= sheetSize.Y)
146 {
147 // 将currentFrame.Y置零,回到第一行的位图
148 currentFrame.Y = 0;
149 }
150 }
151 }
152 }
153
154 /// <summary>
155 /// 画精灵的方法
156 /// </summary>
157 /// <param name="gameTime">GameTime</param>
158 /// <param name="spriteBatch">接受外部传来的SpriteBatch参数</param>
159 public virtual void Draw(GameTime gameTime, SpriteBatch spriteBatch)
160 {
161 spriteBatch.Draw(textureImage, position, new Rectangle(currentFrame.X * frameSize.X, currentFrame.Y * frameSize.Y, frameSize.X, frameSize.Y), Color.White, 0, Vector2.Zero, 1f, SpriteEffects.None, 0);
162 }
163
164 #endregion
165 }
166 }

posted on 2011-04-12 12:42  Osiris_Syou  阅读(238)  评论(0编辑  收藏  举报

导航