Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I'm using XNA to make a Bullet Hell Shooter game, and I'm running into some troubles when I try to change the direction in which the bullets move. Been trying to debug for awhile, but haven't gotten anywhere.

The effect that I am trying to have is

I've included all code that might have any dealing with the code.

Custom Box class(really only used for holding the Rectangle and Velocity)

    class Box
    {
        public Rectangle rect;
        public Vector2 velo;
        public Box()
        {
            rect = new Rectangle();
        }

        public Box(Rectangle r, Vector2 v)
        {
            rect = r;
            velo = v;
        }
    }

Global Declaration:

    List<Box> bullets;
    List<int> bulletType;

Bullet Logic:

    public void bulletStuff()
    {
        if (bullets.Count > 100)
        {
            while (bullets.Count > 99)
            {
                bullets.RemoveAt(0);
                bulletType.RemoveAt(0);
            }
        }

        for (int i = 0; i < bullets.Count; i++)
        {
            bullets[i].rect.Y -= (int)bullets[i].velo.Y;
            bullets[i].rect.X += (int)bullets[i].velo.X;

            if (bullets[i].rect.Y < 0)
            {
                bullets.RemoveAt(i);
            }
        }
    }

Collision w/ Enemy & Enemy Logic(Also a Box)

        public void enemyStuff()
        {
            for (int i = 0; i < enemyRects.Count; i++)
            {
                enemyRects[i].rect.Y += (int)enemyRects[i].velo.Y;
                enemyRects[i].rect.X += (int)enemyRects[i].velo.X;

                if (enemyRects[i].rect.Y >= 800)
                {
                    enemyRects.RemoveAt(i);
                    break;
                }

                if (enemyRects[i].rect.Intersects(playerRect))
                {
                    playerHealth--;
                    enemyRects.RemoveAt(i);
                    break;
                }

                for (int j = 0; j < bullets.Count; j++)
                {
                    if(bullets[j].rect.Intersects(enemyRects[i].rect))
                    {
                        bullets.RemoveAt(j);
                        bulletType.RemoveAt(j);
                        enemyHealth[i]--;
                    }
                }

                if (enemyHealth[i] <= 0)
                {
                    enemyRects.RemoveAt(i);
                    enemyHealth.RemoveAt(i);
                    enemiesKilled++;
                    break;
                }
            }
        }

Update Method

            if ((curKeyState.IsKeyDown(Keys.Space) && !preKeyState.IsKeyDown(Keys.Space)) || (curMouseState.LeftButton == ButtonState.Pressed && preMouseState.LeftButton == ButtonState.Pressed))
            {//Spawning the Elements
                Box Temp;
                if (level == 0)
                {
                    Temp = new Box();
                    Temp.rect = new Rectangle(playerRect.X + (playerRect.Width / 2) - 2, playerRect.Y - (playerRect.Height / 2), 5, 5);
                    Temp.velo = new Vector2(0, 10);
                    bullets.Add(Temp);
                    bulletType.Add(0);
                }
                else if (level == 1)
                {
                    Temp = new Box();
                    Temp.rect = new Rectangle(playerRect.X + (playerRect.Width / 2) - 2, playerRect.Y - (playerRect.Height / 2), 10, 10);
                    Temp.velo = new Vector2(-2, 10);
                    bullets.Add(Temp);
                    bulletType.Add(1);
                    Temp.velo = new Vector2(2, 10);
                    bullets.Add(Temp);
                    bulletType.Add(2);
                }
                else if (level == 2)
                {
                    Temp = new Box();
                    Temp.rect = new Rectangle(playerRect.X + (playerRect.Width / 2) - 2, playerRect.Y - (playerRect.Height / 2), 10, 10);
                    Temp.velo = new Vector2(-2, 10);
                    bullets.Add(Temp);
                    bulletType.Add(1);
                    Temp.velo = new Vector2(2, 10);
                    bullets.Add(Temp);
                    bulletType.Add(2);
                }
                else
                {
                    Temp = new Box();
                    Temp.rect = new Rectangle(playerRect.X + (playerRect.Width / 2) - 2, playerRect.Y - (playerRect.Height / 2), 10, 10);
                    Temp.velo = new Vector2(-2, 10);
                    bullets.Add(Temp);
                    bulletType.Add(1);
                    Temp.velo = new Vector2(2, 10);
                    bullets.Add(Temp);
                    bulletType.Add(2);
                }

            }

            playerRect.X = curMouseState.X - playerRect.Width / 2;
            playerRect.Y = curMouseState.Y - playerRect.Height / 2;

            levelStuff();
            bulletStuff();
            enemyStuff();

            if (playerHealth == 0)
            {
                ScreenManager.Instance.addScreen(new TitleScreen(myGame)); //Exits to menu
            }

            base.Update(gameTime);
        }

What is occurring currently is this:

enter image description here

As you can see, it is spawning both new bullets(which are supposed to go opposite of each other) moving in the same direction.

share|improve this question
    
What debugging steps have you taken already? –  Byte56 Feb 22 '13 at 0:19
    
@Byte56 I've already redone the spawning code, as well as tried to figure out where the second bullet spawned is. I discovered that they are spawning in the exact same place, although, they are not moving in the same direction. In the picture I drew the bullets by number, but they overlap, thus I'm led to believe that the vaie of "Temp" when spawning bullets does not change after I reassign the value of it's X velocity to 2, instead of -2. –  jatmdm Feb 22 '13 at 0:21
2  
Even though you're adding the bullet to the list, if you modify it after that, you're still modifying the original. So from the looks of it, you're adding two bullets every time both of type two and with a velocity of (2, 10). You have to create a new bullet and modify its value if you want to have different bullets. BTW, I'm voting to close this as too localized. This isn't the place for people to debug your code for you. –  Byte56 Feb 22 '13 at 0:24
    
Thanks, that helps. I'll try that now. –  jatmdm Feb 22 '13 at 0:24
    
Thank you very much @Byte56 , it worked just as you said. I appreciate the help. –  jatmdm Feb 22 '13 at 0:27
add comment

closed as too localized by Byte56, Sean Middleditch, Josh Petrie, Trevor Powell, bummzack Feb 23 '13 at 10:45

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

1 Answer

up vote 4 down vote accepted

Allow me to comment a little bit of your code:

Temp = new Box(); //create a new box
Temp.rect = new Rectangle(playerRect.X + (playerRect.Width / 2) - 2,
  playerRect.Y - (playerRect.Height / 2), 10, 10); //set its size and position
Temp.velo = new Vector2(-2, 10); //set its velocity to (-2, 10)
bullets.Add(Temp); //add it to the bullets list
bulletType.Add(1); //add a type...
Temp.velo = new Vector2(2, 10); //now change its velocity (this is still accessing the object stored in the list).
bullets.Add(Temp); //add it AGAIN to the list. Now we have two bullets with a velocity of (2, 10)
bulletType.Add(2); //add a type.
share|improve this answer
add comment

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