简体   繁体   中英

Rotate and Rescale Sprite XNA Mid-Game

I just begun learning XNA 4.0 and I have a question. I created a sprite and I need to rotate it when a specific event occurs. I would also like to know how to rescale that same sprite while in the middle of the game. I tried this:

Texture2D theSprite;

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

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

        if (direction=="right")
        {
            spriteBatch.Draw(theSprite, spritePosition, null, Color.White, 0, new Vector2(0, 0), .7f, SpriteEffects.None, 0); 
        }

But it changes the sprite's position and I'm guessing there has to be a better way, right?

Thanks!

It looks it change position because rotation center is at vector(0,0), you have to change this to be in the middle of object.

Origin = new Vector2(texture.Width / 2, texture.Height / 2);
Rotate = MathHelper.ToRadians(45)
Scale = 0.7f

spriteBatch.Draw(theSprite, spritePosition, null, Color.White, Rotate, Origin, Scale , SpriteEffects.None, 0);

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM