以源码的形式介绍XNA框架中如何播放动画序列
测试环境:VS2010SP1+WP7.1开发工具
需要的图片,或,代码中有不明白的地方,请参考下面的网址
http://www.cnblogs.com/TerryBlog/archive/2011/03/23/1992912.html
下面是我的源码,是根据上面网址中的内容自己归纳为一个C#源文件,方便以后自己查询
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.Input.Touch; using Microsoft.Xna.Framework.Media; namespace testPNG { /// <summary> /// This is the main type for your game /// </summary> public class Game1 : Microsoft.Xna.Framework.Game { GraphicsDeviceManager graphics; SpriteBatch spriteBatch; Point frameSize = new Point(150, 150); //每帧的长与高 Point currentFrame = new Point(0, 0); //初始化第一帧 Point sheetSize = new Point(10, 1); //定义一个10 列1行的point ,本图片一共有一列10个小人 Texture2D myTexture = null; public Game1() { graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; // Frame rate is 30 fps by default for Windows Phone. //TargetElapsedTime = TimeSpan.FromTicks(333333); TargetElapsedTime = TimeSpan.FromTicks(333333*3);//改为10fps // Extend battery life under lock. InactiveSleepTime = TimeSpan.FromSeconds(1); } /// <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); // [1]文件要放在XXXContent目录下,其实XXX是你的项目名,XXXContent目录由向导(Wizard)自动建立 // [2]PlayerMagic.png要放在“Solution Explorer”窗口“testPNGContent(Content)”节点下 // [3]纹理可以支持的格式 .bmp、.dds、.dib、.hdr、.jpg、.pfm、.png、.ppm、和 .tga // [4]加入文件后,看它的"Assert Name"属性,下面函数的入口参数为PlayerMagic.png文件“Assert Name”属性的值(在Properties窗口中) myTexture = Content.Load<Texture2D>("PlayerMagic"); } /// <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 if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed) this.Exit(); // TODO: Add your update logic here ++currentFrame.X; //将下标要画的X坐标剃增,即改变该列的位置向下个位置转移 if (currentFrame.X >= sheetSize.X) //如果下标大于或者等于系列图的当前列的数量 { currentFrame.X = 0; //重新将下标初始化为0 } 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(Color.CornflowerBlue); // TODO: Add your drawing code here spriteBatch.Begin(SpriteSortMode.FrontToBack, BlendState.AlphaBlend); spriteBatch.Draw(myTexture,//纹理图像 new Vector2((graphics.GraphicsDevice.Viewport.Width / 2) - (frameSize.X / 2), (graphics.GraphicsDevice.Viewport.Height / 2) - (frameSize.Y / 2)), //将系列图放在屏幕中间播放 new Rectangle( currentFrame.X * frameSize.X,//当前的帧点的x 轴乘以每行移动的宽度得到该矩形从屏幕哪个坐标画 currentFrame.Y * frameSize.Y, //当前的帧点的Y 轴乘以每行移动的高度得到该矩形从屏幕哪个坐标画 frameSize.X,//需要画该矩形块的宽度为系列图每格小人物的宽度 frameSize.Y),//需要画该矩形块的高度为系列图每格小人物的高度 Color.White, 0, //旋转图像,按角度旋转图像,0为不旋转,依次类似1、2、3、4、5等 Vector2.Zero,//指定旋转的参照点 1, //缩放比例,这里是按默认比例绽放 SpriteEffects.None, //不翻转图像 0 //纹理层深度 ); spriteBatch.End(); base.Draw(gameTime); } } }
下面是我在代码中用到的图片(PlayerMagic.png文件),大小为1500*150象素