XNA / c#我不能让我的射弹一次射击一次并完成他们各自的生命周期如果我释放空格键

时间:2012-02-25 23:32:25

标签: c#-4.0 xna-4.0

感谢阅读。 问题:在空格键按下后,无论是按下另一个按键还是放开空格键,我都需要使用我的射弹进行射击并继续其生命。

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

下面的AddProjectile代码

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

更新下面的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();
                }

所以,这就是我到目前为止所说的并且如上所述可以让抛射物完好无损,我只是不能让它们继续它们的生命(直到碰撞或它们到达屏幕的末端)一旦我放开了键盘空格键。 任何帮助都将不胜感激 感谢您再次阅读。 。 gruffy

1 个答案:

答案 0 :(得分:3)

编辑:刚刚意识到这是几个月前被问到的。 OOPS。我会留下我的答案,如果有人会发现它有用。

好吧,我可以告诉你的问题在于你的UpdateProjectiles()代码。具体在:

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

这意味着当您没有按空格键时,您将删除列表中的所有射弹。同样在您的绘图代码中,当空格键被释放时,您将专门调用UpdateProjectiles(),这将清除您的射弹列表。

你可以做的是,如果弹丸被展示,而不是让空格键控制,而是分开管理。我的意思是这样的:

//adds projectiles - only if the spacebar was pressed this update, 
//ensuring that only ONE projectile is added per press:

if (currentKeyboardState.IsKeyDown(Keys.Space) && previousKeyboardState.IsKeyUp(Keys.Space))
{
    projectiles.Add(new Projectile());
}

//updates the projectiles:

for(int i = 0; i < projectiles.Count; ++i)
{
    projectiles[i].Update();
}

//checks if projectiles have collided or are out of bounds:

foreach(projectile p in projectiles)
{
    if (PROJECTILE IS OUT OF BOUNDS || HAVE COLLIDED WITH STUFF)
    {
        DO WHAT NEEDS TO BE DONE;
        projectiles.Remove(p);
        break;
    }
}

正如你所看到的,空格键在这里唯一做的就是在按下时添加新的射弹。无论空格键是否被按下,都会更新和移除抛射物。 同样在您的绘图代码中,我建议将其更改为:

//draws the projectiles regardless of whether the spacebar is pressed
foreach (projectile p in projectiles)
{
    p.Draw(spriteBatch);
}

这意味着无论是否按下空格键,您都将在列表中绘制所有抛射物​​(如果它是空的,则不绘制)。我还建议移动UpdateProjectiles()只能在你的Update()调用中,因为这会释放你的绘图方法,只是为了绘制内容。

我并不是说这是最有效的方法,但它会完成工作。但需要注意的一点是,你只能在UpdateProjectiles()次呼叫中删除1个射弹,但这不应该是一个问题(至少在我的经验中),因为你正在更新许多一秒钟,在一次更新中需要移除两个射弹的情况很少见。

希望这会有所帮助:)