C#XNA - 如何使用2个Rectangle变量绘制动画纹理?

时间:2014-11-12 18:25:33

标签: c# animation xna rectangles sprite-sheet

我遇到了一个需要解决的问题。我正在C#XNA中制作太空入侵者游戏。我一直在关注入侵者的本教程:https://www.youtube.com/watch?list=LLhsF03QfDMDhbw02_8C6Smg&v=wIW4S75iZBI&feature=player_detailpage#t=397 但我也有自己的代码来动画Invader spritesheet。最终,这就是我所画的内容:

void Draw(SpriteBatch spriteBatch){
  for (int r = 0; r < m_InvaderRows; r++)
  {
    for (int c = 0; c < m_InvaderCollumns; c++)
    {
      spriteBatch.Draw(m_BotInvaderTex, m_BotInvaderPos, m_BotInvaderHitBox, Color.White, 0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);

      // ???

      spriteBatch.Draw(m_BotInvaderTex, m_BotInvadersRect[r, c], Color.White);
    }
  }
}

因此,第一个Draw()调用仅绘制了1个动画精灵。第二个绘制了50个非动画精灵。 m_BotInvadersRect[r, c]是我用作多维数组的矩形。因此,我正在寻找一种方法来合并Draw个调用,并在屏幕上绘制50个动画精灵。

如何使用下面的代码为Invaders设置动画?

我的代码:

    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using Microsoft.Xna.Framework;
    using Microsoft.Xna.Framework.Storage;
    using Microsoft.Xna.Framework.Audio;
    using Microsoft.Xna.Framework.Content;
    using Microsoft.Xna.Framework.GamerServices;
    using Microsoft.Xna.Framework.Graphics;
    using Microsoft.Xna.Framework.Input;
    using Microsoft.Xna.Framework.Media;
    using Microsoft.Xna.Framework.Net;

    namespace SpaceInvaders
    {
    class botInvader
    {

        public botInvader()
        {

        }

        public static Texture2D g_BotInvaderTex;
        Rectangle m_BotInvaderHitBox;
        public static Vector2 g_BotInvaderPos = new Vector2(0, 24);
        Vector2 m_BotInvaderOrigin;

        int m_BotInvaderCurrentFrame = 1;
        int m_BotInvaderFrameWidth = 52;
        int m_BotInvaderFrameHeight = 88;

        float m_Timer = 0f;
        float m_Interval = 100;

        Rectangle[,] m_BotInvadersRect;
        int m_InvaderRows = 5;
        int m_InvaderCollumns = 10;
        String m_BotInvadersDirection = "RIGHT";

        public void Initialize()
        {

        }

        public void LoadContent(ContentManager Content)
        {
            g_BotInvaderTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\botInvaders\\normalInvaders\\spritesheet"); // invaderShip1
            m_BotInvadersRect = new Rectangle[m_InvaderRows, m_InvaderCollumns];
            for (int r = 0; r < m_InvaderRows; r++)
            {
                for (int c = 0; c < m_InvaderCollumns; c++)
                {
                    m_BotInvadersRect[r, c].Width = g_BotInvaderTex.Width;
                    m_BotInvadersRect[r, c].Height = g_BotInvaderTex.Height;
                    m_BotInvadersRect[r, c].X = 70 * c;
                    m_BotInvadersRect[r, c].Y = 70 * r;
                }
            }
        }

        public void Update(GameTime gameTime)
        {
            m_BotInvaderHitBox = new Rectangle(m_BotInvaderCurrentFrame * m_BotInvaderFrameWidth, 0, m_BotInvaderFrameWidth, m_BotInvaderFrameHeight);
            m_BotInvaderOrigin = new Vector2(m_BotInvaderHitBox.X / 2, m_BotInvaderHitBox.Y / 2);

            m_Timer += (float)gameTime.ElapsedGameTime.Milliseconds;

            if (m_Timer > m_Interval)
            {
                m_BotInvaderCurrentFrame++;
                m_Timer = 0f;
            }

            if (m_BotInvaderCurrentFrame == 2)
            {
                m_BotInvaderCurrentFrame = 0;
            }

            m_BotInvaderHitBox = new Rectangle(m_BotInvaderCurrentFrame * m_BotInvaderFrameWidth, 0, m_BotInvaderFrameWidth, m_BotInvaderFrameHeight);
            m_BotInvaderOrigin = new Vector2(m_BotInvaderHitBox.Width / 2, m_BotInvaderHitBox.Height / 2);

            int m_RightSide = 800;
            int m_LeftSide = 0;

            for (int r = 0; r < m_InvaderRows; r++)
            {
                for (int c = 0; c < m_InvaderCollumns; c++)
                {
                    if (m_BotInvadersDirection.Equals ("RIGHT"))
                    {
                        m_BotInvadersRect[r, c].X = m_BotInvadersRect[r, c].X + 1;
                    }

                    if (m_BotInvadersDirection.Equals("LEFT"))
                    {
                        m_BotInvadersRect[r, c].X = m_BotInvadersRect[r, c].X - 1;
                    }
                }
            }

            String m_BotInvadersChangeDirection = "N";

            for (int r = 0; r < m_InvaderRows; r++)
            {
                for (int c = 0; c < m_InvaderCollumns; c++)
                {
                    if (m_BotInvadersRect[r, c].X + m_BotInvadersRect[r, c].Width > m_RightSide)
                    {
                        m_BotInvadersDirection = "LEFT";
                        m_BotInvadersChangeDirection = "Y";
                    }

                    if (m_BotInvadersRect[r, c].X < m_LeftSide)
                    {
                        m_BotInvadersDirection = "RIGHT";
                        m_BotInvadersChangeDirection = "Y";
                    }
                }

                if (m_BotInvadersChangeDirection.Equals("Y"))
                {
                        for (int c = 0; c < m_InvaderCollumns; c++)
                        {
                            m_BotInvadersRect[r, c].Y = m_BotInvadersRect[r, c].Y + 3;
                        }
                }
            }
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            for (int r = 0; r < m_InvaderRows; r++)
            {
                for (int c = 0; c < m_InvaderCollumns; c++)
                {
                    spriteBatch.Draw(g_BotInvaderTex, g_BotInvaderPos, m_BotInvaderHitBox, Color.White, 0f, Vector2.Zero, 1.0f, SpriteEffects.None, 0);
                    spriteBatch.Draw(g_BotInvaderTex, m_BotInvadersRect[r, c], Color.White);
                }
            }         
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您必须像本教程中那样创建一个用于创建精灵的调用 - &gt; enter link description here。在这个类中,yoo也要放动画

 m_BotInvaderHitBox = new Rectangle(m_BotInvaderCurrentFrame * m_BotInvaderFrameWidth, 0, m_BotInvaderFrameWidth, m_BotInvaderFrameHeight);
            m_BotInvaderOrigin = new Vector2(m_BotInvaderHitBox.X / 2, m_BotInvaderHitBox.Y / 2);

            m_Timer += (float)gameTime.ElapsedGameTime.Milliseconds;

            if (m_Timer > m_Interval)
            {
                m_BotInvaderCurrentFrame++;
                m_Timer = 0f;
            }

            if (m_BotInvaderCurrentFrame == 2)
            {
                m_BotInvaderCurrentFrame = 0;
            }

            m_BotInvaderHitBox = new Rectangle(m_BotInvaderCurrentFrame * m_BotInvaderFrameWidth, 0, m_BotInvaderFrameWidth, m_BotInvaderFrameHeight);
            m_BotInvaderOrigin = new Vector2(m_BotInvaderHitBox.Width / 2, m_BotInvaderHitBox.Height / 2);

            int m_RightSide = 800;
            int m_LeftSide = 0;

            for (int r = 0; r < m_InvaderRows; r++)
            {
                for (int c = 0; c < m_InvaderCollumns; c++)
                {
                    if (m_BotInvadersDirection.Equals ("RIGHT"))
                    {
                        m_BotInvadersRect[r, c].X = m_BotInvadersRect[r, c].X + 1;
                    }

                    if (m_BotInvadersDirection.Equals("LEFT"))
                    {
                        m_BotInvadersRect[r, c].X = m_BotInvadersRect[r, c].X - 1;
                    }
                }
            }

            String m_BotInvadersChangeDirection = "N";

            for (int r = 0; r < m_InvaderRows; r++)
            {
                for (int c = 0; c < m_InvaderCollumns; c++)
                {
                    if (m_BotInvadersRect[r, c].X + m_BotInvadersRect[r, c].Width > m_RightSide)
                    {
                        m_BotInvadersDirection = "LEFT";
                        m_BotInvadersChangeDirection = "Y";
                    }

                    if (m_BotInvadersRect[r, c].X < m_LeftSide)
                    {
                        m_BotInvadersDirection = "RIGHT";
                        m_BotInvadersChangeDirection = "Y";
                    }
                }

                if (m_BotInvadersChangeDirection.Equals("Y"))
                {
                        for (int c = 0; c < m_InvaderCollumns; c++)
                        {
                            m_BotInvadersRect[r, c].Y = m_BotInvadersRect[r, c].Y + 3;
                        }
                }
            }

最后你必须按照教程创建一个这个对象的列表,并在draw方法中绘制每个列表项。