C#开发跨平台游戏——游戏中动画速度的调整
上次我们提到如何在游戏中使用多张图片实现动画,细心的朋友会发现动画的播放速度过于快,那么我们如何来控制动画的速度呢?
原理是定义动画的切换时间T1,即多少时间变化一次(当前索引),单位为毫秒。再定义一个动画切换后过了多长时间T2,然后用这个时间T2去和T1去做比较,看是否超过了,如果超过这个时间那么切换到下一张图片。T2恢复到小于每秒切换时间T1,单位为毫秒。
打开Game1.cs,找到【int currentFrame = 0;// 当前图片索引】在下方定义两个变量
1 2 |
int timeLastFrame = 0;//每次切换图片后经过的时间 int timePerFame = 100;//每100毫秒切换一次,默认是16.666毫秒切换一次,即画面刷新率每秒60帧 |
然后找到【Update(GameTime gameTime)】方法,找到上次提到的动画代码,替换为
1 2 3 4 5 6 7 8 9 10 11 12 13 |
timeLastFrame = timeLastFrame + gameTime.ElapsedGameTime.Milliseconds;//图片切换后经过的时间(毫秒) if (timeLastFrame > timePerFame) { timeLastFrame = timeLastFrame - timePerFame;//将图片切换后经过的时间恢复到小于每秒切换时间,保证下面代码执行一次 if (currentFrame >= blacksmithJoyTextures.Count - 1) { currentFrame = 0; } else { currentFrame += 1; } } |
完整代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
using System; using System.Collections.Generic; using System.Linq; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Content; using Microsoft.Xna.Framework.GamerServices; using Microsoft.Xna.Framework.Graphics; using Microsoft.Xna.Framework.Input; using Microsoft.Xna.Framework.Media; namespace HelloWorld.XNA { /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; SpriteFont defaultFont; Color backgoundColor; Texture2D mouseCursor; Vector2 mouseCursorPosition; Texture2D buttonNormal; Texture2D buttonMoveOver; Texture2D buttonPressed; Rectangle buttonRect; Texture2D button; Song backgroundMusic;//定义背景音乐 SoundEffect buttonPressedSound;//定义按钮音效 List<Texture2D> blacksmithJoyTextures;//定义图片列表 Rectangle blacksmithJoyRect;//动画的位置及大小 int currentFrame = 0;// 当前图片索引 int timeLastFrame = 0;//每次切换图片后经过的时间 int timePerFame = 100;//每100毫秒切换一次,默认是16.666毫秒切换一次,即画面刷新率每秒60帧 public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; graphics.PreferredBackBufferWidth = 800; graphics.PreferredBackBufferHeight = 600; graphics.IsFullScreen = false; IsMouseVisible = false; } /// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here base.Initialize(); } /// <summary> /// LoadContent will be called once per game and is the place to load /// all of your content. /// </summary> protected override void LoadContent() { // Create a new SpriteBatch, which can be used to draw textures. spriteBatch = new SpriteBatch(GraphicsDevice); // TODO: use this.Content to load your game content here defaultFont = Content.Load<SpriteFont>("DefaultFont"); backgoundColor = Color.CornflowerBlue; mouseCursor = Content.Load<Texture2D>("MouseCursor"); buttonNormal = Content.Load<Texture2D>("ButtonNormal"); buttonMoveOver = Content.Load<Texture2D>("ButtonMoveOver"); buttonPressed=Content.Load<Texture2D>("ButtonPressed"); buttonRect = new Rectangle(300, 200,128,128); button = buttonNormal; backgroundMusic = Content.Load<Song>("BackgroundMusic");//加载背景音乐 buttonPressedSound = Content.Load<SoundEffect>("ButtonPressedSound");//加载按钮音效 MediaPlayer.IsRepeating = true;//重复播放背景音乐 MediaPlayer.Play(backgroundMusic);//播放背景音乐 //MediaPlayer.Volume = 1;//背景音乐声音大小(0.1f-1.0f) //MediaPlayer.Stop(); blacksmithJoyTextures = new List<Texture2D>(); for(int i=0;i<30;i++) { Texture2D blacksmithJoyTexture; if (i < 10) { blacksmithJoyTexture = Content.Load<Texture2D>("Joy/0_Blacksmith_Joy_00" + i); } else { blacksmithJoyTexture = Content.Load<Texture2D>("Joy/0_Blacksmith_Joy_0" + i); } blacksmithJoyTextures.Add(blacksmithJoyTexture); } blacksmithJoyRect = new Rectangle(100, 100, 200, 200); } /// <summary> /// UnloadContent will be called once per game and is the place to unload /// all content. /// </summary> protected override void UnloadContent() { // TODO: Unload any non ContentManager content here } /// <summary> /// Allows the game to run logic such as updating the world, /// checking for collisions, gathering input, and playing audio. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Update(GameTime gameTime) { // Allows the game to exit KeyboardState keyboardState = Keyboard.GetState(PlayerIndex.One); if(keyboardState.IsKeyDown(Keys.Escape)) { this.Exit(); } if(keyboardState.IsKeyDown(Keys.F10)) { graphics.IsFullScreen = true; graphics.ApplyChanges(); } if(keyboardState.IsKeyDown(Keys.F11)) { graphics.IsFullScreen = false; graphics.ApplyChanges(); } if (keyboardState.IsKeyDown(Keys.F12)) { graphics.PreferredBackBufferWidth = 1920; graphics.PreferredBackBufferHeight = 1080; graphics.ApplyChanges(); } MouseState mouseState = Mouse.GetState();//获取鼠标状态 if(mouseState.LeftButton==ButtonState.Pressed)//判断是否按下了鼠标左键 { backgoundColor = Color.Red;//将背景设置为红色 if (buttonRect.Contains(mouseState.X, mouseState.Y))//判断鼠标是否移动到按钮上并且按下 { button = buttonPressed;//将按钮设置为按下状态 buttonPressedSound.Play();//播放按钮音效 } } else { if (buttonRect.Contains(mouseState.X, mouseState.Y))//判断鼠标是否移动到按钮上 { button = buttonMoveOver;//将按钮设置为悬停状态 } else//鼠标不在按钮上 { button = buttonNormal;//将按钮设置为正常状态 } backgoundColor = Color.CornflowerBlue;//放开鼠标左键恢复成蓝色 } mouseCursorPosition = new Vector2(mouseState.X, mouseState.Y); timeLastFrame = timeLastFrame + gameTime.ElapsedGameTime.Milliseconds;//图片切换后经过的时间(毫秒) if (timeLastFrame > timePerFame) { timeLastFrame = timeLastFrame - timePerFame;//将图片切换后经过的时间恢复到小于每秒切换时间,保证下面代码执行一次 if (currentFrame >= blacksmithJoyTextures.Count - 1) { currentFrame = 0; } else { currentFrame += 1; } } // TODO: Add your update logic here base.Update(gameTime); } /// <summary> /// This is called when the game should draw itself. /// </summary> /// <param name="gameTime">Provides a snapshot of timing values.</param> protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(backgoundColor);//绘制游戏背景 // TODO: Add your drawing code here spriteBatch.Begin(); spriteBatch.DrawString(defaultFont, "这是我的第一个游戏", Vector2.Zero, Color.White); //绘制按钮 spriteBatch.Draw(button, buttonRect, Color.White); spriteBatch.Draw(blacksmithJoyTextures[currentFrame], blacksmithJoyRect, Color.White); spriteBatch.Draw(mouseCursor,mouseCursorPosition,Color.White); spriteBatch.End(); base.Draw(gameTime); } } } |
点击【启动】
运行效果,动画明显变慢了即100毫秒才切换一次。所以改变timePerFame的值就可以改变动画的速度
如果您对C#游戏开发感兴趣,可以扫下面二维码加入我们的QQ群来一起学习交流
原创文章,转载请注明本文链接地址(违者必究):C#开发跨平台游戏——游戏中动画速度的调整