Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/295.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何使用2个矩形变量绘制动画纹理?_C#_Animation_Xna_Rectangles_Sprite Sheet - Fatal编程技术网

C# 如何使用2个矩形变量绘制动画纹理?

C# 如何使用2个矩形变量绘制动画纹理?,c#,animation,xna,rectangles,sprite-sheet,C#,Animation,Xna,Rectangles,Sprite Sheet,我有个问题需要解决。我正在用cxna制作一个太空入侵者游戏。我一直在为入侵者学习本教程: 但我也有自己的代码来制作入侵者精灵表的动画。最后我画的是: void Draw(SpriteBatch spriteBatch){ for (int r = 0; r < m_InvaderRows; r++) { for (int c = 0; c < m_InvaderCollumns; c++) { spriteBatch.Draw(m_BotInvad

我有个问题需要解决。我正在用cxna制作一个太空入侵者游戏。我一直在为入侵者学习本教程: 但我也有自己的代码来制作入侵者精灵表的动画。最后我画的是:

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);
    }
  }
}

您必须创建一个创建精灵的调用,如本教程->。在这节课上,你还得放动画

m_BotInvestrHitbox=新矩形m_BotInvestrReframe*m_BotInvestrFrameWidth,0,m_BotInvestrFrameWidth,m_BotInvestrFrameHeight; m_BotInvestororigin=新载体2M_BotInvestorHitbox.X/2,m_BotInvestorHitbox.Y/2; m_Timer+=floatgameTime.ElapsedGameTime.millides; 如果m_定时器>m_间隔 { m_Botrunframe++; m_定时器=0f; } 如果m_BotRecurrenFrame==2 { m_Botrunframe=0; } m_BotInvestrHitbox=新矩形m_BotInvestrReframe*m_BotInvestrFrameWidth,0,m_BotInvestrFrameWidth,m_BotInvestrFrameHeight; m_BotInvestorRigin=新向量2m_BotInvestorHitbox.Width/2,m_BotInvestorHitbox.Height/2; int m_右侧=800; int m_LeftSide=0; 对于int r=0;rm_right side { m_BotrsDirection=左侧; m_BotrsChangeDirection=Y; } 如果m_BottersRect[r,c].X}在Game1.cs的绘制方法中
    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);
                }
            }         
        }
    }
}