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

Posted

技术标签:

【中文标题】C# XNA - 如何使用 2 个矩形变量绘制动画纹理?【英文标题】:C# XNA - How to draw animated texture with 2 Rectangle variables? 【发布时间】:2015-01-09 17:08:13 【问题描述】:

我有一个问题需要解决。我正在用 C# XNA 制作 Space Invaders 游戏。我一直在关注入侵者的本教程: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() 调用只绘制了一个动画精灵。第二个绘制 50 个非动画精灵。 m_BotInvadersRect[r, c] 是我用作多维数组的矩形。所以我正在寻找一种方法来结合两个 Draw 调用并在我的屏幕上绘制 50 个动画精灵。

如何使用下面的代码为入侵者设置动画?

我的代码:

    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】:

你必须像本教程一样创建一个调用来创建精灵 --> enter link description here。在这个类中你还得放动画

 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 方法中绘制列表中的每个项目。

【讨论】:

Game1.cs的draw方法中

以上是关于C# XNA - 如何使用 2 个矩形变量绘制动画纹理?的主要内容,如果未能解决你的问题,请参考以下文章

XNA - 更新方法与绘制方法

[XNA问题,绘制许多不同批次时

XNA 减速动画

使用 XNA/Monogame 从 spritesheet 中绘制一个 sprite

如何使用XNA编程动画FBX模型

如何以编程方式使用 xna 为 fbx 模型设置动画