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

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 using Microsoft.Xna.Framework.Input;
8
9 namespace AnimatedSprites
10 {
11 /// <summary>
12 /// 用户控制的精灵类
13 /// </summary>
14 class UserControlledSprite : Sprite
15 {
16 #region ■变量声明______________________________________________________________
17
18 /// <summary>
19 /// 用来检测鼠标
20 /// </summary>
21 private MouseState prevMouseState;
22
23 #endregion
24
25 #region ■属性声明______________________________________________________________
26
27 /// <summary>
28 /// 用来让用户的精灵往这个方向移动的属性
29 /// </summary>
30 public override Vector2 direction
31 {
32 get
33 {
34 Vector2 inputDirection = Vector2.Zero;
35 if (Keyboard.GetState().IsKeyDown(Keys.Left))
36 {
37 // 按左让X-=1
38 inputDirection.X -= 1;
39 }
40 if (Keyboard.GetState().IsKeyDown(Keys.Right))
41 {
42 // 按右让X+=1
43 inputDirection.X += 1;
44 }
45 if (Keyboard.GetState().IsKeyDown(Keys.Up))
46 {
47 // 按上让Y-=1
48 inputDirection.Y -= 1;
49 }
50 if (Keyboard.GetState().IsKeyDown(Keys.Down))
51 {
52 // 按下让Y+=1
53 inputDirection.Y += 1;
54 }
55
56 // XBox的手柄操作
57 GamePadState gamepadState = GamePad.GetState(PlayerIndex.One);
58 if (gamepadState.ThumbSticks.Left.X != 0)
59 inputDirection.X += gamepadState.ThumbSticks.Left.X;
60 if (gamepadState.ThumbSticks.Left.Y != 0)
61 inputDirection.Y += gamepadState.ThumbSticks.Left.Y;
62
63 // 乘上精灵的移动速度再返回
64 return inputDirection * speed;
65 }
66 }
67
68 #endregion
69
70 #region ■构造方法______________________________________________________________
71
72 // 两个构造方法唯一不同的地方是第二个方法需要一个用来计算动画速度的变量millisecondsPerFrame
73
74 /// <summary>
75 /// 构造方法
76 /// </summary>
77 /// <param name="textureImage">要绘制的精灵或精灵位图</param>
78 /// <param name="position">精灵被绘制的位置</param>
79 /// <param name="frameSize">精灵位图中单帧的尺寸</param>
80 /// <param name="collisionOffset">偏移量用来修改精灵碰撞检测中用到的包围矩形</param>
81 /// <param name="currentFrame">偏移量用来修改精灵碰撞检测中用到的包围矩形</param>
82 /// <param name="sheetSize">精灵位图中的行/列数</param>
83 /// <param name="speed">精灵来在X,Y方向移动的速度</param>
84 public UserControlledSprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed)
85 : base(textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, speed)
86 {
87 }
88
89 /// <summary>
90 /// 构造方法
91 /// </summary>
92 /// <param name="textureImage">要绘制的精灵或精灵位图</param>
93 /// <param name="position">精灵被绘制的位置</param>
94 /// <param name="frameSize">精灵位图中单帧的尺寸</param>
95 /// <param name="collisionOffset">偏移量用来修改精灵碰撞检测中用到的包围矩形</param>
96 /// <param name="currentFrame">偏移量用来修改精灵碰撞检测中用到的包围矩形</param>
97 /// <param name="sheetSize">精灵位图中的行/列数</param>
98 /// <param name="speed">精灵来在X,Y方向移动的速度</param>
99 /// <param name="millisecondsPerFrame">帧间等待的时间(毫秒数)</param>
100 public UserControlledSprite(Texture2D textureImage, Vector2 position, Point frameSize, int collisionOffset, Point currentFrame, Point sheetSize, Vector2 speed, int millisecondsPerFrame)
101 : base(textureImage, position, frameSize, collisionOffset, currentFrame, sheetSize, speed, millisecondsPerFrame)
102 {
103 }
104
105 #endregion
106
107 #region ■成员方法______________________________________________________________
108
109 /// <summary>
110 /// 用户精灵的更新方法
111 /// </summary>
112 /// <param name="gameTime">GameTime</param>
113 /// <param name="clientBounds">代表游戏窗口客户区矩形,用来检测物体何时越过了游戏窗口边缘</param>
114 public override void Update(GameTime gameTime, Rectangle clientBounds)
115 {
116 // 让用户精灵根据指定的方向移动
117 position += direction;
118
119 // 如果精灵跑出了游戏窗口,则让它回到窗口内
120 MouseState currMouseState = Mouse.GetState();
121 if (currMouseState.X != prevMouseState.X ||
122 currMouseState.Y != prevMouseState.Y)
123 {
124 position = new Vector2(currMouseState.X, currMouseState.Y);
125 }
126 prevMouseState = currMouseState;
127
128 // 如果精灵跑出了游戏窗口,则让它回到窗口内
129 if (position.X < 0)
130 {
131 position.X = 0;
132 }
133 if (position.Y < 0)
134 {
135 position.Y = 0;
136 }
137 if (position.X > clientBounds.Width - frameSize.X)
138 {
139 position.X = clientBounds.Width - frameSize.X;
140 }
141 if (position.Y > clientBounds.Height - frameSize.Y)
142 {
143 position.Y = clientBounds.Height - frameSize.Y;
144 }
145 // 调用父类的更新方法
146 base.Update(gameTime, clientBounds);
147 }
148
149 #endregion
150
151 }
152 }

posted on 2011-04-12 12:44  Osiris4Net  阅读(255)  评论(0编辑  收藏  举报

导航