Note that there are some explanatory texts on larger screens.

plurals
  1. POXNA/C# projectiles firing issue - need one projectile at a time to fire on spacebar press
    primarykey
    data
    text
    <p>Problem: Upon spacebar press i need my projectile to fire and continue its life regardless of another press or letting go of the spacebar.</p> <p>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.</p> <p>AddProjectile code below</p> <p>enter code here</p> <pre><code>//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); } } </code></pre> <p>Update Projectile code below</p> <pre><code>private void UpdateProjectiles() { //update projectiles for (int i = projectiles.Count - 1; i &gt;= 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); } } } </code></pre> <p>the Draw method that draws projectiles to screen</p> <pre><code>//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 &lt; projectiles.Count; i++) { projectiles[i].Draw(spriteBatch); } } //remove projectiles and reset if (currentKeyboardState.IsKeyUp(Keys.Space) || currentGamePadState.Buttons.A == ButtonState.Released) { UpdateProjectiles(); } </code></pre> <p>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.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload