【问题标题】:XNA - adding items to a list and draw themXNA - 将项目添加到列表并绘制它们
【发布时间】:2014-12-10 01:45:52
【问题描述】:

所以我在 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);
    }
}

【问题讨论】:

    标签: c# xna


    【解决方案1】:

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

    【讨论】:

    • 同意,注意移除的条件是 Y >= -632,这很可能总是为真。
    • 同意,但如果没有有关 blasterLasers 的更多信息,很难说。我建议设置一些断点,看看是不是这个问题。
    • 那我也要发布我的 blasterLasers 课程。
    • @BradleyDotNET 即使我评论说屏幕上仍然没有绘制任何项目符号。
    • @Charlie 您的列表计数为 0,因此要么没有添加任何内容(设置断点以查看是否到达该代码),要么您将立即删除。如果您没有仔细评论,您可能没有做任何事情...请确认您是在添加而不是删除。
    猜你喜欢
    • 2014-06-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-03
    • 1970-01-01
    • 2023-04-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多