什么是精灵,这绝对不是XNA特有的概念,在FLASH等简单的游戏框架当中都有这个概念,精灵是一个游戏当中可移动的有动画效果的元素。那么举一个最简单的例子。

 

 

举一个最简单的例子,植物大战这个游戏大家都玩过吧,那么这个游戏场早就冰刀中哪些元素是精灵呢?僵尸、植物、阳光、后面的小车都有一个动画元素,那么无疑他们也都是精灵。

好概念就说到这里,如果在XNA的世界里绘制一个精灵呢,其实非常简单,依旧新建一个XNA GAME STUDIO 4.0的工程,名字就叫GAME1吧,咱们之前在框架介绍当中已经说过了GAME1这个工程,但是还有一个工程叫GAME1CONTENT这个他就是这个GAME的资源工程,向这个工程添加文件非常简单

 

读者可以上图另存一下,然后复制,点中GAME1CONTENT然后直接CTRL+V就可以了把这个资源包含到这个游戏里面去了,怎么样挺神奇的吧。

那么接下来我们应该如何来绘制这个图片呢?

很简单在

1 GAME1类当中添加两个个类变量

    public class Game1 : Microsoft.Xna.Framework.Game

    {

        GraphicsDeviceManager graphics;

        SpriteBatch spriteBatch;

        Texture2D texture1;//存放图片的对象

        Vector2 spritePosition1=new Vector2(0, 0);//绘制精灵的位置 

2 在LoadContent方法中把图版载入进来。

 

protected override void LoadContent()

        {

            // Create a new SpriteBatch, which can be used to draw textures.

         

            spriteBatch = new SpriteBatch(GraphicsDevice);

texture1 = Content.Load<Texture2D>("GameThumbnail");

}

接下来在Draw方法中把这个精灵这样画出来。

 protected override void Draw(GameTime gameTime)

        {

                     graphics.GraphicsDevice.Clear(Color.CornflowerBlue);

            // Draw the sprite. 

                       spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);

          spriteBatch.Draw(texture1, spritePosition1, Color.White);//在spritePosition1的位置画texture1

            spriteBatch.End();

                   base.Draw(gameTime);

        }

OK再运行一下你的程序看在出现在左上角的小图片了吧。就这么简单。

那么其实一个静态的图片真的算不上是精灵的感觉,那么我们想想怎么让这个精灵动起来来吧。

在这面程序的基础上再添加一个类变量

     public class Game1 : Microsoft.Xna.Framework.Game

    {

        GraphicsDeviceManager graphics;

        SpriteBatch spriteBatch;

        Texture2D texture1;

            Vector2 spritePosition1new Vector2(0, 0);//绘制精灵的位置;

                Vector2 spriteSpeed1 = new Vector2(50.0f, 50.0f);//精灵的速度。

然后添加这样一个函数

 void UpdateSprite(GameTime gameTime, ref Vector2 spritePosition, ref Vector2 spriteSpeed)

        {

            // Move the sprite by speed, scaled by elapsed time.

            spritePosition +=

                spriteSpeed * (float)gameTime.ElapsedGameTime.TotalSeconds;//根据游戏运行时间改变精灵的位置,大家看等式的右边(float)gameTime.ElapsedGameTime.TotalSeconds就是速度乘以时间再加上原来的位置,就是现在的位置这是基本的几何原理。

            int MaxX =

                graphics.GraphicsDevice.Viewport.Width - texture1.Width;//graphics.GraphicsDevice.Viewport.Width 这个是屏幕的宽度精灵最大的X位置不能超过屏幕

            int MinX = 0;

            int MaxY =

                graphics.GraphicsDevice.Viewport.Height - texture1.Height;//同上

            int MinY = 0;

            // Check for bounce.

            if (spritePosition.X > MaxX)

            {

                spriteSpeed.X *= -1;//如果精灵超出屏幕宽度范围则X轴的速度反向。

                spritePosition.X = MaxX;

            }

            else if (spritePosition.X < MinX)

            {

                spriteSpeed.X *= -1;//同上

                spritePosition.X = MinX;

            }

            if (spritePosition.Y > MaxY)

            {

                spriteSpeed.Y *= -1;;//如果精灵超出屏幕高度范围则Y轴的速度反向。


                spritePosition.Y = MaxY;

            }

            else if (spritePosition.Y < MinY)

            {

                spriteSpeed.Y *= -1;//同上

                spritePosition.Y = MinY;

            }

        }

然后在UPDATE方法中调用上面这个函数更新精灵的位置。

 protected override void Update(GameTime gameTime)

        {

            // Allows the game to exit

            if (GamePad.GetState(PlayerIndex.One).Buttons.Back ==

                ButtonState.Pressed)

                this.Exit();

            // Move the sprite around.

            UpdateSprite(gameTime, ref spritePosition1, ref spriteSpeed1);//更新精灵位置

                       base.Update(gameTime);

        }

好了再运行这个游戏看到了一个会动的精灵了是不是?好让咱们下次更深入的了解一下精灵!