Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/287.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# 子弹与敌人碰撞XNA 4.0_C#_Xna - Fatal编程技术网

C# 子弹与敌人碰撞XNA 4.0

C# 子弹与敌人碰撞XNA 4.0,c#,xna,C#,Xna,大家好,我现在正在做一个项目,其中包括在XNA制作一个简单的平台游戏。我已经到了我要让“子弹”在与敌人或平台碰撞时消失的地步。如果它们与敌人相撞,也会伤害敌人。我似乎找不到我的代码的问题,但当“子弹”碰到平台或敌人时,什么也不会发生。我还得到ArgumentOutOfRange异常是当火球与平台碰撞时未处理的错误 Game1.cs protected void AddFireBall() { // Load fireBallSprite fireB

大家好,我现在正在做一个项目,其中包括在XNA制作一个简单的平台游戏。我已经到了我要让“子弹”在与敌人或平台碰撞时消失的地步。如果它们与敌人相撞,也会伤害敌人。我似乎找不到我的代码的问题,但当“子弹”碰到平台或敌人时,什么也不会发生。我还得到ArgumentOutOfRange异常是当火球与平台碰撞时未处理的错误

Game1.cs

    protected void AddFireBall()
    {
        // Load fireBallSprite
        fireBallSprite = Content.Load<Texture2D>("Sprites/fireball");

        // Fire in the player's current moving direction
        if (player.FacingRight == true)
        {
            FireBallsRight.Add(new FireBall(fireBallSprite, new Vector2(player.Position.X + (player.Texture.Width / 2), player.Position.Y)));
        }
        else if (player.FacingRight == false)
        {
            FireBallsLeft.Add(new FireBall(fireBallSprite, new Vector2(player.Position.X - (player.Texture.Width / 2), player.Position.Y)));
        }

    }
    // Update fireball position
    protected void UpdateFireBall()
    {
        // Fireballs moving to the right
        for (int i = 0; i < FireBallsRight.Count; i++)
        {
            FireBallsRight[i].MoveRight();

            // If the fireball goes outside the map,
            // remove fireball from list
            if (FireBallsRight[i].Position.X + FireBallsRight[i].Texture.Width > screenBound.Width)
            {
                FireBallsRight.RemoveAt(i);
            }
        }

        // Fireballs moving to the left
        for (int i = 0; i < FireBallsLeft.Count; i++)
        {
            FireBallsLeft[i].MoveLeft();

            // If the fireball goes outside the map,
            // remove fireball from list
            if (FireBallsLeft[i].Position.X + FireBallsLeft[i].Texture.Width > screenBound.Width)
            {
                FireBallsLeft.RemoveAt(i);
            }
        }

    }

    protected void UpdateCollision()
    {
        // Check for collisions between player and platform
        foreach (Platform p in Platforms)
        {
            player = p.CheckIfCollision(player);
        }

        // Use the Rectangle's built-in intersect function to 
        // determine if two objects are overlapping
        Rectangle rectangle1;
        Rectangle rectangle2;
        Rectangle rectangle3;
        Rectangle rectangle4;

        // Create a rectrangle for player
        rectangle1 = new Rectangle((int)player.Position.X, (int)player.Position.Y, player.Texture.Width, player.Texture.Height);

        // Update player score and remove treasure from map when colliding
        for (int i = 0; i < Treasures.Count; i++)
        {
            rectangle3 = Treasures[i].BoundingRectangle;
            // Determine if the two objects collided with each
            // other
            if (rectangle1.Intersects(rectangle3))
            {
                // Add player score based on Treasure's score value
                player.score += Treasures[i].score;

                // Remove the treasure from the list
                Treasures.RemoveAt(i);
            }
        }

        // Kill the player when colliding with DemonEnemy
        for (int i = 0; i < DemonEnemies.Count; i++)
        {
            rectangle2 = DemonEnemies[i].BoundingRectangle;
            if (rectangle1.Intersects(rectangle2))
            {
                player.health = 0;
            }
        }

        // Kill the player when colliding with LordRylinoth
        for (int i = 0; i < LordRylinoths.Count; i++)
        {
            rectangle2 = LordRylinoths[i].BoundingRectangle;
            if (rectangle1.Intersects(rectangle2))
            {
                player.health = 0;
            }
        }

        // Check if fireball intersects with a platform
        for (int i = 0; i < FireBallsRight.Count; i++)
        {
            for (int x = 0; x < Platforms.Count; x++)
            {
                rectangle4 = new Rectangle((int)FireBallsRight[i].Position.X - FireBallsRight[i].Texture.Width, (int)FireBallsRight[i].Position.Y - FireBallsRight[i].Texture.Height,
                FireBallsRight[i].Texture.Width, FireBallsRight[i].Texture.Height);

                rectangle2 = Platforms[x].BoundingRectangle;

                if (rectangle4.Intersects(rectangle2))
                {
                    // Remove fireball from list
                    FireBallsRight.RemoveAt(i);
                }
            }
        }

        // Check if fireball intersects with a platform
        for (int i = 0; i < FireBallsLeft.Count; i++)
        {
            for (int x = 0; x < Platforms.Count; x++)
            {
                rectangle4 = new Rectangle((int)FireBallsLeft[i].Position.X - FireBallsLeft[i].Texture.Width, (int)FireBallsLeft[i].Position.Y - FireBallsLeft[i].Texture.Height, 
                FireBallsLeft[i].Texture.Width, FireBallsLeft[i].Texture.Height);

                rectangle2 = Platforms[x].BoundingRectangle;

                if (rectangle4.Intersects(rectangle2))
                {
                    // Remove fireball from list
                    FireBallsLeft.RemoveAt(i);
                }
            }
        }

        // Check if fireball intersects with demon enemy
        for (int i = 0; i < FireBallsRight.Count; i++)
        {
            for (int x = 0; x < DemonEnemies.Count; x++)
            {
                rectangle4 = new Rectangle((int)FireBallsRight[i].Position.X - FireBallsRight[i].Texture.Width, (int)FireBallsRight[i].Position.Y - FireBallsRight[i].Texture.Height,
                FireBallsRight[i].Texture.Width, FireBallsRight[i].Texture.Height);

                rectangle2 = DemonEnemies[x].BoundingRectangle;

                if (rectangle4.Intersects(rectangle2))
                {
                    // Subtract demon health when fireball hits
                    DemonEnemies[x].health -= FireBallsRight[i].damage;

                    // Remove fireball from list
                    FireBallsRight.RemoveAt(i);

                    /*// Kill the demon when health reaches 0
                    if (DemonEnemies[x].health <= 0)
                    {
                        DemonEnemies.RemoveAt(x);
                    }*/
                } 
            }
        }

        // Check if fireball intersects with demon enemy
        for (int i = 0; i < FireBallsLeft.Count; i++)
        {
            for (int x = 0; x < DemonEnemies.Count; x++)
            {
                rectangle4 = new Rectangle((int)FireBallsLeft[i].Position.X - FireBallsLeft[i].Texture.Width, (int)FireBallsLeft[i].Position.Y - FireBallsLeft[i].Texture.Height,
                FireBallsLeft[i].Texture.Width, FireBallsLeft[i].Texture.Height);

                rectangle2 = DemonEnemies[x].BoundingRectangle;

                if (rectangle4.Intersects(rectangle2))
                {
                    // Subtract demon health when fireball hits
                    DemonEnemies[x].health -= FireBallsLeft[i].damage;

                    // Remove fireball from list
                    FireBallsRight.RemoveAt(i);

                    // Kill the demon when health reaches 0
                    if (DemonEnemies[x].health <= 0)
                    {
                        DemonEnemies.RemoveAt(x);
                    }
                }
            }
        }

    }
class FireBall
{
    public Texture2D Texture;
    public Vector2 Position;
    public Rectangle BoundingRectangle;
    public int damage;
    public float moveSpeed;

    public FireBall(Texture2D Texture, Vector2 Position)
    {
        this.Texture = Texture;
        this.Position = Position;
        BoundingRectangle = new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height);
        damage = 50;
        moveSpeed = 10f;
    }

    public void MoveRight()
    {
        Position.X += moveSpeed;
    }

    public void MoveLeft()
    {
        Position.X -= moveSpeed;
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(Texture, new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height), Color.White);
    }
}
class Enemy
{
    public Texture2D Texture;
    public Vector2 Position;
    public Rectangle BoundingRectangle;
    public int score;
    public int health;
    public float moveSpeed;

    public Enemy(Texture2D Texture, Vector2 Position, int score, int health, float moveSpeed)
    {
        this.Texture = Texture;
        BoundingRectangle = new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height);
        this.score = score;
        this.health = health;
        this.moveSpeed = moveSpeed;
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(Texture, BoundingRectangle, Color.White);
    }
}
敌人.cs

    protected void AddFireBall()
    {
        // Load fireBallSprite
        fireBallSprite = Content.Load<Texture2D>("Sprites/fireball");

        // Fire in the player's current moving direction
        if (player.FacingRight == true)
        {
            FireBallsRight.Add(new FireBall(fireBallSprite, new Vector2(player.Position.X + (player.Texture.Width / 2), player.Position.Y)));
        }
        else if (player.FacingRight == false)
        {
            FireBallsLeft.Add(new FireBall(fireBallSprite, new Vector2(player.Position.X - (player.Texture.Width / 2), player.Position.Y)));
        }

    }
    // Update fireball position
    protected void UpdateFireBall()
    {
        // Fireballs moving to the right
        for (int i = 0; i < FireBallsRight.Count; i++)
        {
            FireBallsRight[i].MoveRight();

            // If the fireball goes outside the map,
            // remove fireball from list
            if (FireBallsRight[i].Position.X + FireBallsRight[i].Texture.Width > screenBound.Width)
            {
                FireBallsRight.RemoveAt(i);
            }
        }

        // Fireballs moving to the left
        for (int i = 0; i < FireBallsLeft.Count; i++)
        {
            FireBallsLeft[i].MoveLeft();

            // If the fireball goes outside the map,
            // remove fireball from list
            if (FireBallsLeft[i].Position.X + FireBallsLeft[i].Texture.Width > screenBound.Width)
            {
                FireBallsLeft.RemoveAt(i);
            }
        }

    }

    protected void UpdateCollision()
    {
        // Check for collisions between player and platform
        foreach (Platform p in Platforms)
        {
            player = p.CheckIfCollision(player);
        }

        // Use the Rectangle's built-in intersect function to 
        // determine if two objects are overlapping
        Rectangle rectangle1;
        Rectangle rectangle2;
        Rectangle rectangle3;
        Rectangle rectangle4;

        // Create a rectrangle for player
        rectangle1 = new Rectangle((int)player.Position.X, (int)player.Position.Y, player.Texture.Width, player.Texture.Height);

        // Update player score and remove treasure from map when colliding
        for (int i = 0; i < Treasures.Count; i++)
        {
            rectangle3 = Treasures[i].BoundingRectangle;
            // Determine if the two objects collided with each
            // other
            if (rectangle1.Intersects(rectangle3))
            {
                // Add player score based on Treasure's score value
                player.score += Treasures[i].score;

                // Remove the treasure from the list
                Treasures.RemoveAt(i);
            }
        }

        // Kill the player when colliding with DemonEnemy
        for (int i = 0; i < DemonEnemies.Count; i++)
        {
            rectangle2 = DemonEnemies[i].BoundingRectangle;
            if (rectangle1.Intersects(rectangle2))
            {
                player.health = 0;
            }
        }

        // Kill the player when colliding with LordRylinoth
        for (int i = 0; i < LordRylinoths.Count; i++)
        {
            rectangle2 = LordRylinoths[i].BoundingRectangle;
            if (rectangle1.Intersects(rectangle2))
            {
                player.health = 0;
            }
        }

        // Check if fireball intersects with a platform
        for (int i = 0; i < FireBallsRight.Count; i++)
        {
            for (int x = 0; x < Platforms.Count; x++)
            {
                rectangle4 = new Rectangle((int)FireBallsRight[i].Position.X - FireBallsRight[i].Texture.Width, (int)FireBallsRight[i].Position.Y - FireBallsRight[i].Texture.Height,
                FireBallsRight[i].Texture.Width, FireBallsRight[i].Texture.Height);

                rectangle2 = Platforms[x].BoundingRectangle;

                if (rectangle4.Intersects(rectangle2))
                {
                    // Remove fireball from list
                    FireBallsRight.RemoveAt(i);
                }
            }
        }

        // Check if fireball intersects with a platform
        for (int i = 0; i < FireBallsLeft.Count; i++)
        {
            for (int x = 0; x < Platforms.Count; x++)
            {
                rectangle4 = new Rectangle((int)FireBallsLeft[i].Position.X - FireBallsLeft[i].Texture.Width, (int)FireBallsLeft[i].Position.Y - FireBallsLeft[i].Texture.Height, 
                FireBallsLeft[i].Texture.Width, FireBallsLeft[i].Texture.Height);

                rectangle2 = Platforms[x].BoundingRectangle;

                if (rectangle4.Intersects(rectangle2))
                {
                    // Remove fireball from list
                    FireBallsLeft.RemoveAt(i);
                }
            }
        }

        // Check if fireball intersects with demon enemy
        for (int i = 0; i < FireBallsRight.Count; i++)
        {
            for (int x = 0; x < DemonEnemies.Count; x++)
            {
                rectangle4 = new Rectangle((int)FireBallsRight[i].Position.X - FireBallsRight[i].Texture.Width, (int)FireBallsRight[i].Position.Y - FireBallsRight[i].Texture.Height,
                FireBallsRight[i].Texture.Width, FireBallsRight[i].Texture.Height);

                rectangle2 = DemonEnemies[x].BoundingRectangle;

                if (rectangle4.Intersects(rectangle2))
                {
                    // Subtract demon health when fireball hits
                    DemonEnemies[x].health -= FireBallsRight[i].damage;

                    // Remove fireball from list
                    FireBallsRight.RemoveAt(i);

                    /*// Kill the demon when health reaches 0
                    if (DemonEnemies[x].health <= 0)
                    {
                        DemonEnemies.RemoveAt(x);
                    }*/
                } 
            }
        }

        // Check if fireball intersects with demon enemy
        for (int i = 0; i < FireBallsLeft.Count; i++)
        {
            for (int x = 0; x < DemonEnemies.Count; x++)
            {
                rectangle4 = new Rectangle((int)FireBallsLeft[i].Position.X - FireBallsLeft[i].Texture.Width, (int)FireBallsLeft[i].Position.Y - FireBallsLeft[i].Texture.Height,
                FireBallsLeft[i].Texture.Width, FireBallsLeft[i].Texture.Height);

                rectangle2 = DemonEnemies[x].BoundingRectangle;

                if (rectangle4.Intersects(rectangle2))
                {
                    // Subtract demon health when fireball hits
                    DemonEnemies[x].health -= FireBallsLeft[i].damage;

                    // Remove fireball from list
                    FireBallsRight.RemoveAt(i);

                    // Kill the demon when health reaches 0
                    if (DemonEnemies[x].health <= 0)
                    {
                        DemonEnemies.RemoveAt(x);
                    }
                }
            }
        }

    }
class FireBall
{
    public Texture2D Texture;
    public Vector2 Position;
    public Rectangle BoundingRectangle;
    public int damage;
    public float moveSpeed;

    public FireBall(Texture2D Texture, Vector2 Position)
    {
        this.Texture = Texture;
        this.Position = Position;
        BoundingRectangle = new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height);
        damage = 50;
        moveSpeed = 10f;
    }

    public void MoveRight()
    {
        Position.X += moveSpeed;
    }

    public void MoveLeft()
    {
        Position.X -= moveSpeed;
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(Texture, new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height), Color.White);
    }
}
class Enemy
{
    public Texture2D Texture;
    public Vector2 Position;
    public Rectangle BoundingRectangle;
    public int score;
    public int health;
    public float moveSpeed;

    public Enemy(Texture2D Texture, Vector2 Position, int score, int health, float moveSpeed)
    {
        this.Texture = Texture;
        BoundingRectangle = new Rectangle((int)Position.X, (int)Position.Y, Texture.Width, Texture.Height);
        this.score = score;
        this.health = health;
        this.moveSpeed = moveSpeed;
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(Texture, BoundingRectangle, Color.White);
    }
}

这是很多代码,请将其缩小到您认为问题发生的地方。你试过调试你的代码吗?在发生冲突的地方设置一个断点,并检查各个值。好的,我更新了代码,使问题应该出现在很多只有您理解的代码和逻辑中。你试过调试吗?我正在调试这个(或其中一个)越界错误可能是由UpdateCollision引起的。当检测到碰撞时,您正在移除火球,但平台循环仍在继续。如果移除最后一个火球,并且存在多个平台,则将再次尝试访问数组索引。当你检测到火球碰撞时,你可以“打破”平台循环。