   for loops and shooting cannons
  I have created this simple ship movement and shooting of a cannon. I completed all of that and it works great, but I want to shoot more then one at a time, so I created a for loop for the variable declaration and every where I use cannon. I still can only shoot once anyone got any ideas. Here is my code that is only pertains to the cannon and shooting.Here is the declarations,#define SHOTS 4int n;for(n = 0; n < SHOTS; n++){//variable declaration for cannon propertiescannon[n].width = 9;cannon[n].height = 9;cannon[n].x = ship.x + 14;cannon[n].y = ship.y - 9;cannon[n].movey = 0;cannon[n].accely = -12;cannon[n].alive = false;}Here is the loop for pressing the fire key,int n;void movement(){for(n = 0; n < SHOTS; n++){//Allows you to shoot only onceif(cannon[n].alive == false){//Check shoot key pressedif(Key_Down(DIK_SPACE)){cannon[n].x = ship.x + 10;cannon[n].y = ship.y - 9;cannon[n].movey -= cannon[n].accely;cannon[n].alive = true;PlaySound(sound_cannon);}}}}this is the update method that handles what happens to the cannon,void cannonUpdate(){int n;for(n = 0; n < SHOTS; n++){//If cannon goes off screen visible = falseif(cannon[n].y < 0){cannon[n].alive = false;}if(cannon[n].alive == true){cannon[n].y -= cannon[n].movey;}else if (cannon[n].alive == false){cannon[n].x = -10;cannon[n].y = -10;cannon[n].movey = 0;}}}
  oh yea here is the drawing of the sprite,for(n = 0; n < SHOTS; n++){    if(cannon[n].alive)    {//draw the shipposition.x = (float)cannon[n].x;position.y = (float)cannon[n].y;sprt->Draw(cannon_image, NULL, NULL, &position, D3DCOLOR_XRGB(255, 255, 255));    }};  Perhaps all 4 shots end up in the exact same spot, so they look like just one?   ;  That was a thought that I had, but I can't seem to draw them differently I have tried many different iterations of what I have and unable to figure this out, got any ideas on how to fix this? I am sure there is probably a simple fix for this, but I am unable to think of this sometimes after coding for hours and hours you start to get a little burnt.   ;  Well,based from this snippet,what Erik Rufelt said is correct.cannon[n].x = ship.x + 10;cannon[n].y = ship.y - 9;Basically you fill all four cannons with the same coordinates,so they would appear on top.Instead,a better solution would be to make you input non-buffered.What I mean is that when you press 'space' for example,you will get multiple key press events and every time your input handler will be called.   You should then put a timer,to limit how fast your shots will be fired.That's it if I undestood your problem correctly.   ;  So if I am understanding you correctly I don't need to change my code just put a time for spacing the shots out when I press SPACE, right? An example would be great.   ;Quote:Original post by wicked357So if I am understanding you correctly I don't need to change my code just put a time for spacing the shots out when I press SPACE, right?Yes. However,first you need to make you input what is called 'immediate' in DirectInput.Since I don't know what kind of input API you are using I cannot help there.However there is also another solution (I think).When you generate your shots you can place them evenly spaced from each other :float cannon_height = some_number;for(n = 0; n < SHOTS; n++){//Allows you to shoot only onceif(cannon[n].alive == false){//Check shoot key pressedif(Key_Down(DIK_SPACE)){cannon[n].x = ship.x + 10;cannon[n].y = (ship.y - 9)-cannon_height*n;cannon[n].movey -= cannon[n].accely;cannon[n].alive = true;PlaySound(sound_cannon);}}As you see that allows you to place each shot before the previous (assuming you are firing on the negative y axis).However this is a lot less flexible,and I still recommend doing it with the first aproach I mentioned.   ;  With the other solution, which was a great idea it doesn't work in the aspect that I want it to. It did however prove that I am firing all shots at the same time, which was already proven in the code theoretically. I think your first suggestion would probably fix it, I did a cannon shooting shots in XNA tutorial, but I tried to incorporate that, but noticed quickly that it is far to different to compare the two. I will have to read up on what I can do here to fix this issue without it getting to complex for me at the time. Thanks for all your help.   ;  Try this. The trick is breaking out of the for loop once we find a single shot that isn't in use. This will fire the four shots over four frames.I've also rearranged things a bit for efficiency.If you don't want holding down the space bar to fire multiple shots then you need to check that it wasn't pressed last frame as well as checking that it is pressed this frame.void movement(){int n;//Check shoot key pressedif(Key_Down(DIK_SPACE)){for(n = 0; n < SHOTS; n++){if(cannon[n].alive == false){cannon[n].x = ship.x + 10;cannon[n].y = ship.y - 9;cannon[n].movey -= cannon[n].accely;cannon[n].alive = true;PlaySound(sound_cannon);break; //Allows you to shoot only once}}}};  That helps out and was something I think I should have caught as well, but at the same time there is still a factor of if you don't tap it fast you shoot like 2 at a time, but thank you for all your help and I will mess with other things too.   