0

Okay so i have an array of bullet objects that i want to updae everytime the game loop runs. When the update method is called i want one bullet to fire when i click the mouse or press a key, however everytime i press the fire button on the keyboard or mouse all of the objects in the array seem to fire, i have managed to do this properly before but for the life of me i cannot figure out where i am going wrong, any suggestions?

Here is the code in the update method of my game:

for (int i = 0; i < maxbullets; i++)
{
    bullets[i].update_bulets(gametime, Position, velocity,
                             rotation, viewport, keystate,
                             gamepadstate, bulletsound);
}
Adi Lester
  • 24,731
  • 12
  • 95
  • 110
ree
  • 59
  • 10

1 Answers1

0

Okay after trolling through all of my old files i was tankfully("tank" pun intended) able to solve it using a little marker to track the amount of times i pressed the fire button.I dont just post questions and answer them btw i just managed to find the answer myself hope someone finds it as useful as i did and i do apologise for the bad formatting. Time to get back to my project :)

        if ((previousgamepadstate.IsButtonUp(Buttons.RightTrigger) && gamepadstate.IsButtonDown(Buttons.RightTrigger))
            || (previouskeyboardstate.IsKeyUp(Keys.F) && keystate.IsKeyDown(Keys.F)))
        {

            bullets[currentbullet].alive = true;

            if (currentbullet < maxbullets - 1)
            {
                currentbullet++;
                bulletsound.Play();
            }
            else
            {
                currentbullet = 0;
            }
        }

        foreach (Bullets bullet in bullets)
        {
            bullet.update_bulets(gametime, Position, velocity, rotation, viewport, bulletsound);
        }
ree
  • 59
  • 10