Bullet vs Enemy Collision XNA 4.0

时间:2014-12-11 17:14:32

标签: c# xna

大家好我正在开展一个项目,包括在XNA中制作一个简单的平台游戏。当他们与敌人或平台相撞时,我已经达到了让“子弹”消失的程度。如果它们与敌人发生碰撞,那么也会伤害敌人。我似乎无法找到我的代码的问题,但当“子弹”触及平台或敌人时没有任何反应。当火球与平台发生碰撞时,我也得到了ArgumentOutOfRangeException未处理的错误。

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

    }

FireBall.cs

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

Enemy.cs

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

0 个答案:

没有答案
相关问题