XNA系列(3)

XNA Game Studio 3.0
How To: Draw a Sprite
Demonstrates how to draw a sprite by using the SpriteBatch class.

The Complete Sample

The code in this topic shows you the technique. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Drawing a Sprite

To draw a sprite on screen

  1. Derive a class from Game.

  2. Define a SpriteBatch object as a field on your game class.

  3. In your LoadContent method, construct the SpriteBatch object, passing the current graphics device.

  4. Load the textures that will be used for drawing sprites in LoadContent.

    In this case, the example uses the Content member to load a texture from the XNA Framework Content Pipeline. The texture must be in the project, with the same name passed to Load.

    private Texture2D SpriteTexture;
    private SpriteBatch ForegroundBatch;
    private Rectangle TitleSafe;
    protected override void LoadGraphicsContent( bool loadAllContent )
    {
        if (loadAllContent)
        {
            ForegroundBatch = new SpriteBatch( graphics.GraphicsDevice );
            SpriteTexture = content.Load<Texture2D>( "Sprite" );
        }
        TitleSafe = GetTitleSafeArea( .8f );
    }
    
  5. In the overridden Draw method, call Clear.

  6. After Clear, call Begin on your SpriteBatch object.

  7. Create a Vector2 to represent the screen position of the sprite.

    On Xbox 360, be careful not to draw foreground sprites on the outer 10 or 20 percent of the screen. Some televisions may obscure the edge of the screen. The GetTitleSafeArea function calculates the area of the current Viewport that is safe, given a specified safety percentage.

        protected Rectangle GetTitleSafeArea( float percent )
        {
            Rectangle retval = new Rectangle( graphics.GraphicsDevice.Viewport.X,
                graphics.GraphicsDevice.Viewport.Y,
                graphics.GraphicsDevice.Viewport.Width,
                graphics.GraphicsDevice.Viewport.Height );
    #if XBOX
            // Find Title Safe area of Xbox 360.
            float border = (1 - percent) / 2;
            retval.X = (int)(border * retval.Width);
            retval.Y = (int)(border * retval.Height);
            retval.Width = (int)(percent * retval.Width);
            retval.Height = (int)(percent * retval.Height);
            return retval;            
    #else
            return retval;
    #endif
        }
    这里计算了显示屏幕的范围,因为有些屏幕的边缘部分是不能显示的。经验阿~
  8. Call Draw on your SpriteBatch object, passing the texture to draw, the screen position, and the color to apply.

  9. Use Color.White to draw the texture without any color effects.

  10. When all the sprites have been drawn, call End on your SpriteBatch object.

    protected override void Draw( GameTime gameTime )
    {
        graphics.GraphicsDevice.Clear( Color.CornflowerBlue );
        ForegroundBatch.Begin();
        Vector2 pos = new Vector2( TitleSafe.Left, TitleSafe.Top );
        ForegroundBatch.Draw( SpriteTexture, pos, Color.White );
        ForegroundBatch.End();
        base.Draw( gameTime );
    }
    
简单来说就是:清空屏幕->确定绘制方位->绘制
对于ForegroundBatch.Begin/End为什么需要我们显示调用?如果是因为Begin可以根据参数来订制显示选项,为什么不直接添加到Draw里面?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值