-4

I've been trying to solve this problem but I really can't see what's wrong with the code. It's exactly the same as my other rectangles but it won't work.

Working Code: Everything in here works fine (more or less)

private void UpdateCollision()
    {
        // Uses built in rectangle intersect function to determine overlapping
        Rectangle rectangle1;
        Rectangle rectangle2;
        Rectangle rectangle3;

        // Creates 1 rectangle for player
        rectangle1 = new Rectangle((int)player.Position.X, (int)player.Position.Y, player.Width, player.Height);

        // Sets collision btw objs
        for (int i = 0; i < enemies.Count; i++)
        {
            for (int j = 0; j < enemies2.Count; j++)
            {
                rectangle2 = new Rectangle((int)enemies[i].Position.X, (int)enemies[i].Position.Y, enemies[i].Width, enemies[i].Height);
                rectangle3 = new Rectangle((int)enemies2[j].Position.X, (int)enemies2[j].Position.Y, enemies2[j].Width, enemies2[j].Height);

                // Determines collision
                if (IntersectPixels(rectangle1, playerTextureData, rectangle2, enemyTextureData) && player.PlayerAnimation.Active == true)
                {
                    player.Health -= enemies[i].Damage;
                    enemies[i].Health = 0;

                    if (player.Health == 0)
                        AddMyExplosion(player.Position);
                }

                if (IntersectPixels(rectangle1, playerTextureData, rectangle3, enemy2TextureData) && player.PlayerAnimation.Active == true)
                {
                    player.Health -= enemies2[j].Damage;
                    enemies2[j].Health = 0;

                    if (player.Health == 0)
                        AddMyExplosion(player.Position);
                }
            }
        }

Non-working Code: The line where the exception pops up is where rectangle3 is defined

// Proj obj collision
        for (int i = 0; i < projectiles.Count; i++)
        {

            for (int j = 0; j < enemies.Count; j++)
            {
                for (int k = 0; k < enemies2.Count; k++)
                {

                    // Creates col det rectangles
                    rectangle1 = new Rectangle((int)projectiles[i].Position.X - projectiles[i].Width / 2, (int)projectiles[i].Position.Y - projectiles[i].Height / 2, projectiles[i].Width, projectiles[i].Height);
                    rectangle2 = new Rectangle((int)enemies[j].Position.X - enemies[j].Width / 2, (int)enemies[j].Position.Y - enemies[j].Height / 2, enemies[j].Width, enemies[j].Height);
                    rectangle3 = new Rectangle((int)enemies2[k].Position.X - enemies2[k].Width / 2, (int)enemies[k].Position.Y - enemies2[k].Height / 2, enemies2[k].Width, enemies2[k].Height);
                    //Rectangle 3 is not working//

                    // Determines col
                    if (IntersectPixels(rectangle1, projectileTextureData, rectangle2, enemyTextureData))
                    {
                        enemies[j].Health -= projectiles[i].Damage;
                        AddImpact(projectiles[i].Position);
                        impactSound.Play();
                        projectiles[i].Active = false;
                    }

                    if (IntersectPixels(rectangle1, projectileTextureData, rectangle3, enemy2TextureData))
                    {
                        enemies2[j].Health -= projectiles[i].Damage;
                        AddImpact(projectiles[i].Position);
                        impactSound.Play();
                        projectiles[i].Active = false;
                    }
                }
            }
        }
    }

So if anyone could help me solve this I would be very grateful.

2
  • 4
    Please tell us what is not working. Also, please shorten the code.
    – usr
    Jun 3, 2012 at 20:27
  • 1
    In that failing line shouldn't (int)enemies[k].Position.Y be (int)enemies2[k].Position.Y ?
    – rene
    Jun 3, 2012 at 20:29

1 Answer 1

2

Typo. Change this line:

rectangle3 = new Rectangle((int)enemies2[k].Position.X - enemies2[k].Width / 2, (int)enemies[k].Position.Y - enemies2[k].Height / 2, enemies2[k].Width, enemies2[k].Height);

to this:

rectangle3 = new Rectangle((int)enemies2[k].Position.X - enemies2[k].Width / 2, (int)enemies2[k].Position.Y - enemies2[k].Height / 2, enemies2[k].Width, enemies2[k].Height);
2
  • Taking the time to read and verify all that code makes you a hero.
    – usr
    Jun 3, 2012 at 20:38
  • 2
    I've actually always been pretty good at spotting minor copy/paste errors like this - I think it's analogous to how in WWI they would send color-blind men up in observation planes, because they could easily spot colored camouflage. Jun 3, 2012 at 20:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.