0
votes

Continuing with my game here (a game where the player is a ship and you shoot down meteors). I am currently working on the ship shooting bullets and I am trying to remove the meteors as they get hit by the bullets. So here's what I've done. I am almost there, but I have found an error in my coding. In my game, there spawns meteors outside the right side of the map and they travel to the left and the point is to shoot them down, this is working if you shoot the meteors in right order but not otherwise. Let me explain it with a picture. Example

If I were to shoot down the second meteor, the meteor marked with number 1 would get destroyed first

//spawns the enemies.
public void LoadEnemies()
    {
        int randY = random.Next(100, 500);
        if (spawn > 1)
        {
            spawn = 0;
            if (enemies.Count() < 4)
                enemies.Add(new Enemies(Content.Load<Texture2D>("meteor"), new Vector2(1110, randY)));
        }
        //Here's where the error lies because of the bulletcolliding (I think)
        for (int i = 0; i < enemies.Count; i++)
        {
            if (!enemies[i].isVisible || bulletColliding)
            {
                bulletColliding = false;
                enemies.RemoveAt(i);
                i--;
            }
        }
    }

Collision method.

        public void bulletCollision(GameTime gameTime)
    {
        foreach (var x in bullets)
        {
            foreach (var y in enemies)
            {
                enemy_rect = new Rectangle((int)y.position.X, (int)y.position.Y, 10, 10);
                bullet_rect = new Rectangle((int)x.position.X, (int)x.position.Y, 10, 10);
                if (bullet_rect.Intersects(enemy_rect))
                {
                    bulletColliding = true;
                }
            }
        }
    }

Basically, I am clueless on how to remove the specific meteor that gets hit, and I need your help. I Appreaciate all help I get.

2

2 Answers

5
votes

DonĀ“t use only bool bulletColliding. Use also integer which shows the number of enemy which should be destroyed and in each enemy class keep information of his number.

The second option can be keeping a bool ifDestroy in enemy class which will be false initially, and change it to true if the enemy should be destroyed. You can check it in loadEnemies().

2
votes

Basically this is a further explanation of Sabrina Le's answer.

You want the enemy objects themselves to have a bool called bulletColliding and in your collision code it would look more like...

public void bulletCollision(GameTime gameTime)
{
    foreach (var x in bullets)
    {
        foreach (var y in enemies)
        {
            enemy_rect = new Rectangle((int)y.position.X, (int)y.position.Y, 10, 10);
            bullet_rect = new Rectangle((int)x.position.X, (int)x.position.Y, 10, 10);
            if (bullet_rect.Intersects(enemy_rect))
            {
                y.bulletColliding = true;
            }
        }
    }
}

and then your enemy loading code would look more like...

//spawns the enemies.
public void LoadEnemies()
{
    int randY = random.Next(100, 500);
    if (spawn > 1)
    {
        spawn = 0;
        if (enemies.Count() < 4)
            enemies.Add(new Enemies(Content.Load<Texture2D>("meteor"), new Vector2(1110, randY)));
    }
    //Here's where the error lies because of the bulletcolliding (I think)
    for (int i = 0; i < enemies.Count; i++)
    {
        if (!enemies[i].isVisible || enemies[i].bulletColliding)
        {
            enemies[i].bulletColliding = false;
            enemies.RemoveAt(i);
            i--;
        }
    }
}

By strictly having a single bulletColliding variable, what is happening is when any meteor is hit, then it is set to true. So when your method checks the meteors sequentially it sees that bulletColliding is set to true even when it checks the first object, and the code inside the if runs. Using enemy specific booleans will let you know which enemy got hit, and will be removed accordingly.