新建一个XNA 游戏

添加 Texture2D maoTexture2D 变量

构造函数中添加

            this.graphics.PreferredBackBufferHeight = 720;
            this.graphics.PreferredBackBufferWidth = 1280;

初始化函数中

            this.IsMouseVisible = true;

添加一个纹理

在LoadContent函数内 加载纹理

maoTexture2D = Content.Load<Texture2D>("mao");

在Draw函数中

            spriteBatch.Begin(SpriteSortMode.BackToFront,BlendState.AlphaBlend);
            spriteBatch.Draw(maoTexture2D, new Vector2(20, 10), Color.White);//one
            spriteBatch.Draw(maoTexture2D, new Rectangle(400, 10, 100, 100), Color.White);//two

            spriteBatch.Draw(maoTexture2D, new Vector2(20, 250), null, Color.White);//one
            spriteBatch.Draw(maoTexture2D, new Vector2(400, 250), new Rectangle(50,50,255,165), Color.White);//three
            spriteBatch.Draw(maoTexture2D, new Rectangle(550,10,100,100), new Rectangle(50, 50, 255, 165), Color.White);//four

            spriteBatch.Draw(maoTexture2D, new Rectangle(50,490,100,100), new Rectangle(60,60,240,160), 
                Color.White, MathHelper.PiOver4 / 2, new Vector2(0, 0), SpriteEffects.None, 1f);//five

            spriteBatch.Draw(maoTexture2D, new Vector2(750, 10), new Rectangle(60, 60, 240, 160),
                Color.White, MathHelper.PiOver4 / 2, new Vector2(0, 0), 2f, SpriteEffects.None, 0.5f);//six

            spriteBatch.Draw(maoTexture2D, new Vector2(750, 360), new Rectangle(60, 60, 200, 100), 
                Color.White, -MathHelper.PiOver4 / 2, new Vector2(0, 0), new Vector2(2f,1.5f), 
                SpriteEffects.FlipVertically|SpriteEffects.FlipHorizontally, 1);//seven

            spriteBatch.End();

程序执行效果

 

 

SpriteBatch的Draw函数有七个重载方式,这七个重载方式可以让简单的2D效果很容易实现.

 

1. Draw(Texture2D texture, Vector2 position, Color color);

 

第一个参数很显然就是2D纹理了,第二个参数是一个2维向量,这里也就是一对Float的数,用于指定纹理的坐标,第三个参数也就是颜色遮罩,一张图换了颜色遮罩就会产生不同颜色的效果。

 

2. Draw(Texture2D texture, Rectangle destinationRectangle, Color color);

 

这个重载实际上就是用一个矩形(第二个参数)代替了2维向量,大家都知道矩形有坐标点还有宽和高,这里用矩形不仅用矩形的坐标替代了Vector2并且指定了纹理的大小(矩形的宽高),这样他就有了缩放功能了。

 

 

3. Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color);

4. Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color);

 

3,4两个重载实际上就比上面重载多了一个Rectangle?带问号的参数在C#里的可空类型(指可以设为Null的参数),这个矩形的作用是用于指绘制整个纹理的某一个部分,如果传入Null的话 就是整张图都显示。

 

 

5.Draw(Texture2D texture, Rectangle destinationRectangle, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, SpriteEffects effects, float layerDepth);

 

第一个Float指的是纹理顺时针旋转度数,注意:里面的度数不是角度而是弧度,所以360°不只旋转一圈,既然要旋转肯定得要一个固定点,这个固定点就是接着的Vector2,这里你可以想象那个点就是一个钉子固定着纹理然后进行旋转。这里又要注意了:这个点不只是为了旋转,这个点不同,也会影响到纹理的放大缩小还有旋转,你可以自己用手去固定矩形(你可以用一本书)然后按住不同点就能看到他旋转的变化(特注:这个点是载入图片内部的坐标系的位置,不是对于屏幕的坐标系)。下一个参数SpriteEffects是一个Enum类型是设置纹理左右映射和上下映射的(也就是有镜像的效果)这里可以枚举多个中间用|分开便可,最后一个参数是又是一个Float他是设置纹理的深度,只能设置0到1之间的浮点数,此参数会影响到纹理画的顺序,0是最前面1是最后面,然后根据SpriteBatch.Begin里的SpriteSortMode来决定画的顺序,当画面上有很多纹理的时候就需要好好规划下这个参数了。

 

6. Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, float scale, SpriteEffects effects, float layerDepth);

 

7. Draw(Texture2D texture, Vector2 position, Rectangle? sourceRectangle, Color color, float rotation, Vector2 origin, Vector2 scale, SpriteEffects effects, float layerDepth);

 

第6个和第7个重载在倒数第三个位置加了一个缩放参数,因为第二个参数被改为Vector2没有缩放功能了,所以就在后面加了这个缩放参数,这个参数可以是Float也可以是一个Vector2,Float是等比缩放,Vector2是按不同的比例缩放。

源代码下载

https://files.cnblogs.com/xuehuzaifei/SpriteBatch_Draw.rar

 

posted on 2013-04-18 13:34  竟飞工作室  阅读(573)  评论(0编辑  收藏  举报