1
\$\begingroup\$

I have implemented a shooting mechanic into my game, and the act of shooting works fine. The shooting work is done in my "game1" class. The issue that arises is when I incorporate collision detection with the bullets and my enemies(zombies). For some reason, I can press shoot all day but none of the zombies will recognize the bullet. It isn't until I press jump with my sprite, that the bullets begin to register and kill the zombies. And jumping is the only way that I can get it to work, moving left or right doesn't do anything. I am going to post the relevant code below.

Zombie Collision code:

public void checkBulletCollision(List<Bullet> bullets)
{
   foreach (Bullet b in bullets)
   {
       if (BoundingBox.Intersects(b.BoundingBox))
       {
           isAlive = false;
           b.isVisible = false;
           bullets.Remove(b);
           break;
       }
   }
}

Shooting code in main game file:

public void UpdateBullets()
{
    foreach (Bullet b in bullets)
    {
        b.position.X += 10;
        if (b.position.X > 1020)
            b.isVisible = false;
    }
    for (int i = 0; i < bullets.Count; i++)
    {
        if (!bullets[i].isVisible)
        {
            bullets.RemoveAt(i);
            i--;
        }
    }
}    

public void Shoot()
{
    Bullet newB = new Bullet(this);
    newB.LoadContent();
    newB.position = player1.position;
    newB.position.Y -= 10;
    newB.position.X += 10;
    newB.isVisible = true;
    if (bullets.Count < 1000)
        bullets.Add(newB);
}

Update code in main game file:

 protected override void Update(GameTime gameTime)
    {
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        // TODO: Add your update logic here
        float deltaTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
        controls.Update();
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
            Exit();

        //update all blocks in array
        for (int i = 0; i < blocks.Length; i++)
        {
            blocks[i].Update();
        }

        //block1.Update();
        //block2.Update();
        //block3.Update();
        //CheckCollisions();
        player1.Update(controls, gameTime, blocks);

        foreach (Zombie z in zombies)
        {
            if (z.isAlive)
            {

                z.Update(gameTime, blocks);

                z.getPlayerPosition(player1);
                z.checkBulletCollision(bullets);
            }

        }
        if (Keyboard.GetState().IsKeyDown(Keys.Space) && pastKey.IsKeyUp(Keys.Space))
        {
            Shoot();

        }

        UpdateBullets();

        pastKey = Keyboard.GetState();

        base.Update(gameTime);
    }

What could be the reason for this?

\$\endgroup\$
3
  • 1
    \$\begingroup\$ Is your Y position properly initialized (=bullets are physically at different y -> jump could assign it)? \$\endgroup\$
    – wondra
    Commented Jul 31, 2014 at 22:09
  • \$\begingroup\$ The y position of my player or the bullets? \$\endgroup\$
    – taytay
    Commented Jul 31, 2014 at 22:18
  • \$\begingroup\$ Player, since they are both derived from it. \$\endgroup\$
    – wondra
    Commented Jul 31, 2014 at 22:20

1 Answer 1

1
\$\begingroup\$

Thanks to @wondra, he told me to look at the initialization of the y variable.

But this fixed the issue-

removing the line: newB.position.Y -= 10; from Shoot()

I guess the bullet became initialized when I was up -10 pixels.

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .