Bagikan melalui


How to: Rotate a Sprite

This article demonstrates how to rotate a sprite around its center.

The source code in this example presumes that the texture being loaded is 64×64 pixels.

To draw a rotated sprite on screen

  1. Follow steps 1–4 of How to: Draw a Sprite.

  2. Determine the screen location of the sprite, and the point within the texture that will serve as the origin. By default, the origin of a texture is (0,0), the upper-left corner. When a sprite is drawn, the origin point in the texture is placed on the screen coordinate specified by the at parameter. In this example, the origin is the center of the texture, and the screen position is the center of the screen.

    private Texture2D SpriteTexture;
    

private SpriteBatch ForegroundBatch; private Vector2 origin; private Vector2 screenpos; protected override void LoadGraphicsContent( bool loadAllContent ) { if (loadAllContent) { // TODO: Load any ResourceManagementMode.Automatic content. ForegroundBatch = new SpriteBatch( graphics.GraphicsDevice ); SpriteTexture = content.Load<Texture2D>( "vipership" ); Viewport viewport = graphics.GraphicsDevice.Viewport; origin.X = SpriteTexture.Width / 2; origin.Y = SpriteTexture.Height / 2; screenpos.X = viewport.Width / 2; screenpos.Y = viewport.Height / 2; }

// TODO: Load any ResourceManagementMode.Manual content.

}

  1. In your Update method, determine the rotation angle to use for the sprite. The angle is specified in radians, and it can be greater than two times pi, but does not need to be.

    private float RotationAngle = 0f;
    

protected override void Update( GameTime gameTime ) { ... // TODO: Add your update logic here.

// The time since Update was called last.
float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

// TODO: Add your game logic here.
RotationAngle += elapsed;
float circle = MathHelper.Pi * 2;
RotationAngle = RotationAngle % circle;

base.Update( gameTime );

}

  1. In your own Draw method, call SpriteBatch.Draw with the texture, angle, screen position, and origin of the texture.

    protected override void Draw( GameTime gameTime )
    

{ graphics.GraphicsDevice.Clear( Color.CornflowerBlue );

// TODO: Add your drawing code here.
ForegroundBatch.Begin();
ForegroundBatch.Draw( SpriteTexture, screenpos, null, Color.White, RotationAngle,
    origin, 1.0f, SpriteEffects.None, 0f );
ForegroundBatch.End();

base.Draw( gameTime );

}

  1. When all the sprites have been drawn, call End on your SpriteBatch object.
using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Storage;
using Microsoft.Xna.Framework.Content;
public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    ContentManager content;


    public Game1()
    {
        graphics = new GraphicsDeviceManager( this );
        content = new ContentManager( Services );
    }


    protected override void Initialize()
    {
        // TODO: Add your initialization logic here.

        base.Initialize();
    }
    private Texture2D SpriteTexture;
    private SpriteBatch ForegroundBatch;
    private Vector2 origin;
    private Vector2 screenpos;
    protected override void LoadGraphicsContent( bool loadAllContent )
    {
        if (loadAllContent)
        {
            // TODO: Load any ResourceManagementMode.Automatic content.
            ForegroundBatch = new SpriteBatch( graphics.GraphicsDevice );
            SpriteTexture = content.Load<Texture2D>( "vipership" );
            Viewport viewport = graphics.GraphicsDevice.Viewport;
            origin.X = SpriteTexture.Width / 2;
            origin.Y = SpriteTexture.Height / 2;
            screenpos.X = viewport.Width / 2;
            screenpos.Y = viewport.Height / 2;
        }

        // TODO: Load any ResourceManagementMode.Manual content.
    }


    protected override void UnloadGraphicsContent( bool unloadAllContent )
    {
        if (unloadAllContent == true)
        {
            content.Unload();
        }
    }

    private float RotationAngle = 0f;
    protected override void Update( GameTime gameTime )
    {
        // Allows the default game to exit on Xbox 360 and Windows.
        if (GamePad.GetState( PlayerIndex.One ).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        // TODO: Add your update logic here.

        // The time since Update was called last.
        float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;

        // TODO: Add your game logic here.
        RotationAngle += elapsed;
        float circle = MathHelper.Pi * 2;
        RotationAngle = RotationAngle % circle;

        base.Update( gameTime );
    }


    protected override void Draw( GameTime gameTime )
    {
        graphics.GraphicsDevice.Clear( Color.CornflowerBlue );

        // TODO: Add your drawing code here.
        ForegroundBatch.Begin();
        ForegroundBatch.Draw( SpriteTexture, screenpos, null, Color.White, RotationAngle,
            origin, 1.0f, SpriteEffects.None, 0f );
        ForegroundBatch.End();

        base.Draw( gameTime );
    }
}

See Also

Tasks

How to: Draw a Sprite

Concepts

2D Graphics Overview

Reference

SpriteBatch
Draw
Texture2D