XNA / C#射弹发射问题 - 一次需要一个射弹在空格键击发射

时间:2012-02-26 22:10:30

标签: c xna-4.0

问题:在空格键按下后,无论是按下另一个按键还是放开空格键,我都需要射击并继续其生命。

下面是代码添加,更新和绘制射弹。我已尝试过对此代码的大量重写,但是我没有足够的经验来找到合适的解决方案。

下面的AddProjectile代码

在这里输入代码

//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);
   }
}

更新下面的Projectile代码

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);
        }



    }

}

将射弹吸引到屏幕的绘制方法

//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();
            }

所以,这就是我到目前为止所说的并且如上所述可以让抛射物完好无损,我只是不能让它们继续它们的生命(直到碰撞或它们到达屏幕的末端)一旦我放开了键盘空格键。

1 个答案:

答案 0 :(得分:1)

快速概述我看到的导致您的问题的问题:

  • 您正在检查在添加,更新和绘制方法期间是否按下了键/按钮。
  • 如果未按任何键,则表示您在更新期间删除该项目,而不是在平局期间绘制该项目。

为了实现我认为你想要完成的目标,我会按照这些方针做一些事情(当然,根据你的需要量身定制和清理):

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);
   }
}

您的空格键或A按钮仅用于触发新的射弹。你想让那个射弹飞到它击中某些东西或飞离屏幕的一侧,它不应该取决于你是否按下按钮才能这样做。也不应该抓住子弹。

有意义吗?