gruffy321
gruffy321

Reputation: 185

XNA/C# projectiles firing issue - need one projectile at a time to fire on spacebar press

Problem: Upon spacebar press i need my projectile to fire and continue its life regardless of another press or letting go of the spacebar.

below is both the code adding, updating, and drawing the projectiles. I have tried numerous reworkings of this code but alas am not experienced enough to have found a suitable solution yet.

AddProjectile code below

enter code here

//add projectile if spacebar is pressed

private void AddProjectile(Vector2 position)
{
    //i need somthing like this to make bullets autofire on phone
    //while (TouchPanel.IsGestureAvailable)
    //{
    //    Projectile projectile = new Projectile();
    //    projectile.Initialize(GraphicsDevice.Viewport, projectileTexture, position);
    //    projectiles.Add(projectile);
    //}

   if (currentKeyboardState.IsKeyDown(Keys.Space) ||
     currentGamePadState.Buttons.A == ButtonState.Pressed)
   {
       Projectile projectile = new Projectile();
       projectile.Initialize(GraphicsDevice.Viewport, projectileTexture, position);
       projectiles.Add(projectile);
   }
}

Update Projectile code below

private void UpdateProjectiles()
{
    //update projectiles
    for (int i = projectiles.Count - 1; i >= 0; i--)
    {

        if (currentKeyboardState.IsKeyDown(Keys.Space) ||
            currentGamePadState.Buttons.A == ButtonState.Pressed)
        {

           //adds particle to first projectile but not again until the next fire butotn press
            //particleEngine.EmitterLocation = new Vector2(projectiles[i].Position.X, projectiles[i].Position.Y);
            projectiles[i].Update();
            projectileOn = true;


        }
        //if projectiles not being fired remove them from the game screen 
        else
        {
            projectiles.RemoveAt(i);
        }



    }

}

the Draw method that draws projectiles to screen

//draw the projectiles
            //***********************************************************
             //using the if here allows control of projectiles to pass...
             //...to the "currentstate of the spacebar (is pressed = fire)
             /***/

            if (currentKeyboardState.IsKeyDown(Keys.Space) ||
                currentGamePadState.Buttons.A == ButtonState.Pressed)
            {
                for (int i = 0; i < projectiles.Count; i++)
                {
                    projectiles[i].Draw(spriteBatch);

                }
            }

            //remove projectiles and reset
            if (currentKeyboardState.IsKeyUp(Keys.Space) ||
                currentGamePadState.Buttons.A == ButtonState.Released)
            {


                UpdateProjectiles();
            }

So, this is what i have so far and as stated can get the projectile to worl fine , i just cant get them to continue their life (until a collision or they reach the end of screen) once i let go of the keyboard spacebar.

Upvotes: 1

Views: 647

Answers (1)

Josh Coulter
Josh Coulter

Reputation: 143

A quick overview of the problems I see that are causing your isses:

  • You're checking if keys/buttons are pressed during your add, update, and draw methods.
  • If a key isn't pressed, you're removing the item during an update and not drawing it during a draw.

To accomplish what I think you're trying to accomplish, I'd do something along these lines (tailored and cleaned up for your needs, of course):

private void Update(GameTime gameTime)
{
   // Only add a new projectile if Space or A is pressed
   if (currentKeyboardState.IsKeyDown(Keys.Space) ||
      currentGamePadState.Buttons.A == ButtonState.Pressed)
   {
      AddProjectile(); // some method that adds a new projectile to 'projectiles'
   }

   // Update all existing projectiles, regardless of button press.
   // This will allow your projectiles to continue flying after they have been fired.
   for (int i = 0; i < projectiles.Count; i++)
   {
      projectiles[i].Update();
      if (projectiles[i].Dead) // check if projectiles[i] is out of bounds or has collided with something
      {
         projectiles.RemoveAt(i);
         i--;
      }
   }
}

private void Draw(GameTime gameTime)
{
   // Draw all existing projectiles, regardless of button press.
   for (int i = 0; i < projectiles.Count; i++)
   {
      projectiles[i].Draw(spriteBatch);
   }
}

Your space bar or A button is only used to fire a new projectile. You want that projectile to fly until it hits something or flies off the side of the screen, and it shouldn't depend on whether you have a button pressed in order to do that. Neither should drawing the bullet.

Make sense?

Upvotes: 1

Related Questions