C# 4.0 XNA/c#如果我释放空格键,我无法让我的射弹一次发射一枚并完成各自的寿命

C# 4.0 XNA/c#如果我释放空格键,我无法让我的射弹一次发射一枚并完成各自的寿命,c#-4.0,xna-4.0,C# 4.0,Xna 4.0,谢谢你的阅读。 问题:当按下空格键时,我需要我的射弹发射并继续它的生命,而不管是否再次按下或放开空格键 下面是添加、更新和绘制投射物的代码。 我已经尝试过多次修改这段代码,但我还没有足够的经验找到合适的解决方案 添加下面的代码 enter code here //add projectile if spacebar is pressed private void AddProjectile(Vector2 position) { //i need somt

谢谢你的阅读。 问题:当按下空格键时,我需要我的射弹发射并继续它的生命,而不管是否再次按下或放开空格键

下面是添加、更新和绘制投射物的代码。 我已经尝试过多次修改这段代码,但我还没有足够的经验找到合适的解决方案

添加下面的代码

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



        }

    }
更新下面的射弹代码

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);
       }
    }
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();
                }
//绘制投射物
//***********************************************************
//在这里使用if可以控制射弹通过。。。
//…至空格键的“当前”状态(按下=点火)
/***/
if(currentKeyboardState.IsKeyDown(Keys.Space)||
currentGamePadState.Buttons.A==ButtonState.Pressed)
{
对于(int i=0;i
所以,这就是我到目前为止所拥有的,正如我所说的,我可以让弹丸运转良好,我只是不能让它们在我放开键盘空格键后继续它们的生命(直到碰撞或它们到达屏幕的尽头)。 任何帮助都将不胜感激 谢谢你再次阅读。
.gruffy

编辑:几个月前才意识到这是一个问题。哎呀。如果有人发现我的答案有任何用处,我会留下我的答案

好的,据我所知,你的问题在于你的
updateprojectles()
code。具体是:

//if projectiles not being fired remove them from the game screen 
        else
        {
            projectiles.RemoveAt(i);
        }
这意味着,当您不按空格键时,您将删除列表中的所有投射物。此外,在绘图代码中,您正在调用
updateprojectles()
,特别是在释放空格键时,这将清除投射物列表

你可以做的是,如果投射物正在显示,不要让空格键控制,而是分开管理。我的意思是这样的:

//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;
    }
}
这意味着,无论是否按下空格键,您都将绘制列表中的所有投射物(如果列表为空,则无任何投射物)。我还建议将
updateprojectles()
移动到您的
Update()
调用中,因为这将释放您的绘制方法,使其仅用于绘制内容

我并不是说这是最有效的方法,但它会完成工作。不过需要注意的是,每次调用
UpdateProjectiles()
时,您只能删除1个投射物,但这应该不是问题(至少在我的经验中没有)由于每秒要更新多次,因此在一次更新中很少需要删除两个射弹


希望这有帮助:)

编辑:几个月前才意识到这是一个问题。哎呀。如果有人发现我的答案有用,我会留下我的答案

好的,据我所知,你的问题在于你的
updateprojectles()
code。具体是:

//if projectiles not being fired remove them from the game screen 
        else
        {
            projectiles.RemoveAt(i);
        }
这意味着,当您不按空格键时,您将删除列表中的所有投射物。此外,在绘图代码中,您正在调用
updateprojectles()
,特别是在释放空格键时,这将清除投射物列表

你可以做的是,如果投射物正在显示,不要让空格键控制,而是分开管理。我的意思是这样的:

//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;
    }
}
这意味着,无论是否按下空格键,您都将绘制列表中的所有投射物(如果列表为空,则无任何投射物)。我还建议将
updateprojectles()
移动到您的
Update()
调用中,因为这将释放您的绘制方法,使其仅用于绘制内容

我并不是说这是最有效的方法,但它会完成工作。不过需要注意的是,每次调用
UpdateProjectiles()
时,您只能删除1个投射物,但这应该不是问题(至少在我的经验中没有)由于每秒要更新多次,因此在一次更新中很少需要删除两个射弹

希望这有帮助:)