XNA - 将项添加到列表并绘制它们

时间:2014-12-09 16:53:21

标签: c# xna

所以我在XNA中制作了一个Space Invaders克隆。我创建了入侵者数组并添加了他们的移动逻辑。我想让他们射子弹。所以我一直在关注它的教程,只使用我需要的代码。但仍然没有在屏幕上绘制子弹。这是我的入侵者类,我删除了与该问题无关的内容:

class botInvaders
{

    public botInvaders(Texture2D newBulletTex)
    {
        bulletsList = new List<blasterLasers>();
        bulletTex = newBulletTex;
        botInvadersHealth = 5;
        currentDificultyLevel = 1;
        bulletDelay = 40;
        isVisible = true;
    }

    public static Texture2D botInvaderTex, bulletTex;
    public static Rectangle botInvaderHitBox;
    public static Vector2 botInvaderOrigin;
    public int botInvaderCurrentFrame = 1, botInvaderFrameWidth = 52, botInvaderFrameHeight = 90, bulletDelay, botInvadersHealth, currentDificultyLevel, invaderRows = 3, invaderCollumns = 10; // invaderRows = 5 // For 50 invaders
    public static Rectangle[,] botInvadersRect;
    public bool isVisible;
    public List<blasterLasers> bulletsList;

    public void LoadContent(ContentManager Content)
    {
        botInvaderTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\botInvaders\\normalInvaders\\invaderShip1");
        bulletTex = Content.Load<Texture2D>(".\\gameGraphics\\gameSprites\\botInvaders\\normalInvaders\\botInvaderLaser");
        botInvadersRect = new Rectangle[invaderRows, invaderCollumns];
    }

    public void Update(GameTime gameTime)
    {
        for (int r = 0; r < invaderRows; r++)
        {
            for (int c = 0; c < invaderCollumns; c++)
            {
                EnemyShoot();
                UpdateBullets();
            }
        }
    }

    public void Draw(Texture2D invadersTex, Rectangle[,] invadersDestinationRect, Nullable<Rectangle> invadersSourceRect, Color invadersColor, float invadersRotation, Vector2 invadersOrigin, SpriteEffects invadersEffects, float invadersScale, SpriteBatch spriteBatch)
    {
        for (int r = 0; r < invaderRows; r++)
        {
            for (int c = 0; c < invaderCollumns; c++)
            {
                spriteBatch.Draw(botInvaderTex, botInvadersRect[r, c], botInvaderHitBox, Color.White);
                foreach (blasterLasers bulletSpawn in bulletsList)
                {
                    bulletSpawn.Draw(spriteBatch);
                }
            }
        }
    }

    public void UpdateBullets()
    {
        foreach (blasterLasers bulletsSpawn in bulletsList)
        {
            bulletsSpawn.bulletPos.Y = bulletsSpawn.bulletPos.Y + bulletsSpawn.bulletSpeed;
            if (bulletsSpawn.bulletPos.Y >= -632)
            {
                bulletsSpawn.isVisible = false;
            }
        }

        for (int i = 0; i < bulletsList.Count(); i++)
        {
            if (!bulletsList[i].isVisible)
            {
                bulletsList.RemoveAt(i);
                i--;
            }
        }
    }

    public void EnemyShoot()
    {
        if (bulletDelay >= 0)
        {
            bulletDelay--;
        }

        if (bulletDelay <= 0)
        {
            blasterLasers newBullet = new blasterLasers(bulletTex);
            newBullet.bulletPos = new Vector2(botInvaderHitBox.X + botInvaderFrameWidth / 2 - newBullet.bulletTex.Width / 2, botInvaderHitBox.Y + 90);
            newBullet.isVisible = true;
            if (bulletsList.Count() < 20)
            {
                bulletsList.Add(newBullet);
            }
        }

        if (bulletDelay == 0)
        {
            bulletDelay = 40;
        }
    }
}

我在Game1中初始化类:

// Create a var
botInvaders botInvader;
// Init it
botInvader = new botInvaders(botInvaders.bulletTex);
// Load Content
botInvader.LoadContent(Content);
// Update
botInvader.Update(gameTime);
// Draw Invaders
botInvader.Draw(botInvaders.botInvaderTex, botInvaders.botInvadersRect, botInvaders.botInvaderHitBox, Color.White, 0f, botInvaders.botInvaderOrigin, SpriteEffects.None, 1.0f, spriteBatch);

问题可能在于我实际上并没有拔出子弹吗? 或者我没有在列表中添加任何项目符号? 如果我调试我可以看到:

bulletsList Count = 0
_size 0
_items [0] null [1] null [2] null [3] null

编辑:

blasterLasers类:

public class blasterLasers
{
    public Texture2D bulletTex;
    public Vector2 bulletOrigin, bulletPos;
    public bool isVisible;
    public float bulletSpeed;
    public Rectangle boundingBox;

    public blasterLasers(Texture2D newBulletTex)
    {
        bulletSpeed = 10f;
        bulletTex = newBulletTex;
        isVisible = false;
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(bulletTex, bulletPos, Color.White);
    }
}

1 个答案:

答案 0 :(得分:0)

当您调用UpdateBullets时,看起来您正在从bulletslist中删除项目符号,因此当您调用Draw时,它会循环通过bulletList,它是空的,因此不会绘制任何内容。