[翻译]XNA外文博客文章精选之three
PS:自己翻译的,转载请著明出处
XNA-旋转一个精灵
自转一个精灵比它前面的版本可容易多了!
许多向往常一样的组件
1 using System;
2 using System.Collections.Generic;
3 using Microsoft.Xna.Framework;
4 using Microsoft.Xna.Framework.Audio;
5 using Microsoft.Xna.Framework.Components;
6 using Microsoft.Xna.Framework.Graphics;
7 using Microsoft.Xna.Framework.Input;
8 using Microsoft.Xna.Framework.Storage;
让我们加入有用的代码!
2 using System.Collections.Generic;
3 using Microsoft.Xna.Framework;
4 using Microsoft.Xna.Framework.Audio;
5 using Microsoft.Xna.Framework.Components;
6 using Microsoft.Xna.Framework.Graphics;
7 using Microsoft.Xna.Framework.Input;
8 using Microsoft.Xna.Framework.Storage;
1 namespace SimpleXNA
2 {
3 partial class Game1 : Microsoft.Xna.Framework.Game
4 {
我已经修改了这个例子,在这个窗口里去绘制一个旋转图象,这样,在这里添加下面的代码:
2 {
3 partial class Game1 : Microsoft.Xna.Framework.Game
4 {
1 SpriteBatch spriteRenderer;
2 Texture2D pTex;
3 float fRot = 0.0f; // this will hold the rotation of the sprite
我也同样创建了一个加载一个纹理的功能:
2 Texture2D pTex;
3 float fRot = 0.0f; // this will hold the rotation of the sprite
1 public Texture2D LoadTexture(string strPath)
2 {
3 TextureInformation information = Texture.GetTextureInformation(strPath);
4 //Not in the cache - need to read it in
5 IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameServices.GetService(typeof(IGraphicsDeviceService));
6 Texture2D tex = Texture2D.FromFile(graphicsService.GraphicsDevice, strPath, information.Width, information.Height);
7 return tex;
8 }
你应该挂起这个Exiting事件,这样你可以释放任何你创建的资源。2 {
3 TextureInformation information = Texture.GetTextureInformation(strPath);
4 //Not in the cache - need to read it in
5 IGraphicsDeviceService graphicsService = (IGraphicsDeviceService)GameServices.GetService(typeof(IGraphicsDeviceService));
6 Texture2D tex = Texture2D.FromFile(graphicsService.GraphicsDevice, strPath, information.Width, information.Height);
7 return tex;
8 }
通过查看Game1.Designer.cs文件实现
在这个方法中,我添加代码为了挂起Starting和Exiting事件:
1 private void InitializeComponent()
2 {
3 this.graphics = new Microsoft.Xna.Framework.Components.GraphicsComponent();
4 this.Starting += new System.EventHandler<Microsoft.Xna.Framework.GameEventArgs>(this.WindowsGame_Starting);
5 this.Exiting += new System.EventHandler<Microsoft.Xna.Framework.GameEventArgs>(this.WindowsGame_Exiting);
6 this.GameComponents.Add(this.graphics);
7 }
回到Game1.cs文件,我们创建Exiting和Starting方法:2 {
3 this.graphics = new Microsoft.Xna.Framework.Components.GraphicsComponent();
4 this.Starting += new System.EventHandler<Microsoft.Xna.Framework.GameEventArgs>(this.WindowsGame_Starting);
5 this.Exiting += new System.EventHandler<Microsoft.Xna.Framework.GameEventArgs>(this.WindowsGame_Exiting);
6 this.GameComponents.Add(this.graphics);
7 }
我们应该释放我们在退出里创建的这个资源
1 private void WindowsGame_Exiting(object sender, GameEventArgs e)
2 {
3 spriteRenderer.Dispose();
4 pTex.Dispose();
5 }
当游戏加载,创建一个纹理并且调整窗口大小去适合纹理的大小。
2 {
3 spriteRenderer.Dispose();
4 pTex.Dispose();
5 }
1 private void WindowsGame_Starting(object sender, GameEventArgs e)
2 {
3 pTex = LoadTexture(@"spiral.jpg");
4 graphics.BackBufferFormat = SurfaceFormat.Color;
5 graphics.BackBufferWidth = 800;
6 graphics.BackBufferHeight = 600;
7 graphics.IsFullScreen = false;
8 graphics.SynchronizeWithVerticalRetrace = false;
9 graphics.AllowMultiSampling = true;
10 graphics.ApplyChanges();
11 spriteRenderer = new SpriteBatch(graphics.GraphicsDevice);
12 }
我在Game1结构代码中没有做修改
2 {
3 pTex = LoadTexture(@"spiral.jpg");
4 graphics.BackBufferFormat = SurfaceFormat.Color;
5 graphics.BackBufferWidth = 800;
6 graphics.BackBufferHeight = 600;
7 graphics.IsFullScreen = false;
8 graphics.SynchronizeWithVerticalRetrace = false;
9 graphics.AllowMultiSampling = true;
10 graphics.ApplyChanges();
11 spriteRenderer = new SpriteBatch(graphics.GraphicsDevice);
12 }
1 public Game1()
2 {
3 InitializeComponent();
4 }
这个更新功能是通过应用程序向导产生的。2 {
3 InitializeComponent();
4 }
在这里,你会处理任何逻辑,它可以处理你的游戏绘制的每一祯:
1 protected override void Update()
2 {
3 // The time since Update was called last
4 float elapsed = (float)ElapsedTime.TotalSeconds;
5 // TODO: Add your game logic here
6 // Let the GameComponents update
7 UpdateComponents();
8 }
这个Draw()方法是一个很好的去绘制场景的地方2 {
3 // The time since Update was called last
4 float elapsed = (float)ElapsedTime.TotalSeconds;
5 // TODO: Add your game logic here
6 // Let the GameComponents update
7 UpdateComponents();
8 }
我已经添加SpriteRenderer.Draw去绘制图象在游戏窗口里面:
1 protected override void Draw(GameTime gameTime)
2 {
3 GraphicsDevice.Clear(Color.CornflowerBlue);
4 spriteRenderer.Begin();
让我们增加旋转角2 {
3 GraphicsDevice.Clear(Color.CornflowerBlue);
4 spriteRenderer.Begin();
在elapsed时间里更新旋转
1 float fRot = (float)gameTime.TotalTime.TotalSeconds;
现在,我们绘制精灵并让它按找我们想要的方式旋转:
1 spriteRenderer.Draw(pTex,
2 //this is the destination point we want to render at
3 new Vector2(this.Window.ClientWidth/2, this.Window.ClientHeight/2),
4 //this is the texture surface area we want to use when rendering
5 new Rectangle(0,0,pTex.Width,pTex.Height),
6 //By using White we are basically saying just use the texture color
7 Color.White,
8 //Here is the rotation in radians
9 fRot,
10 //This is the center point of the rotation
11 new Vector2(pTex.Width/2, pTex.Height/2),
12 //This is the scaling of the sprite. I want it a bit larger so you dont
13 //see the edges as it spins around
14 1.5f,
15 //We are not doing any fancy effects
16 SpriteEffects.None,0);
17 spriteRenderer.End();
18 base.Draw(gameTime);
2 //this is the destination point we want to render at
3 new Vector2(this.Window.ClientWidth/2, this.Window.ClientHeight/2),
4 //this is the texture surface area we want to use when rendering
5 new Rectangle(0,0,pTex.Width,pTex.Height),
6 //By using White we are basically saying just use the texture color
7 Color.White,
8 //Here is the rotation in radians
9 fRot,
10 //This is the center point of the rotation
11 new Vector2(pTex.Width/2, pTex.Height/2),
12 //This is the scaling of the sprite. I want it a bit larger so you dont
13 //see the edges as it spins around
14 1.5f,
15 //We are not doing any fancy effects
16 SpriteEffects.None,0);
17 spriteRenderer.End();
18 base.Draw(gameTime);
源代码:http://www.ziggyware.com/readarticle.php?article_id=42
(完)