Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

I've finally gotten this laser thing to work for my Space Shooter, and so far I've come across a slight problem. I've gotten the laser to position itself at the starting position correctly and move vertically perfectly. Anyway,whenever I hit the space-bar(which is the controls for shooting), it shoots the bullet/laser out, but I have to wait until the bullet clears to shoot another one.

What I want it to do is keep producing bullets until the user releases their hand on the space-bar... here's the code I have so far:

    if(spacebar)
    // this bool variable is set to true once the user presses the space bar
    {

    int frame2 = 0;


    if(once2 == 0)
    {
        projectilex2 = x + 19;
        projectiley2 = y + -35;
        //Positions the bullet at the nose of the ship
    }

    if(frame2 == 3)
    {
        frame2 = 0;

    }

    apply_surface(projectilex2,projectiley2,ShootStuff,screen,&lazers[0]);


    frame2 ++;
    projectiley2--;


    once2++;
    if(once2 == 250)
    {
        once2 = 0;
    spacebar = false;
    }
 //This will eventually get better. My shoot function is horrible lol :/
}

Does anyone have any ideas as to how I can accomplish this without having to wait for the bullet to clear before I can shoot another one :( ?

share|improve this question
2  
May be a different problem, but the same answer i gave you last week solves this problem too! gamedev.stackexchange.com/questions/22616/sdl-bullet-movement/… –  Gustavo Maciel Jan 25 '12 at 14:24
add comment

1 Answer

up vote 2 down vote accepted

You really need to have a separate class for your bullets! It will make everything 1000x easier to manage, render and ultimately to answer your question, the ability to keep adding in new bullets when the space bar is held down.

Your class can be as simple as the following: http://www.allegro.cc/forums/thread/585625

You will need to add some form of update function to the class to manage the movement and ultimately the collision of your bullet, and if you look at the thread it also shows you a bit further down how to remove bullets from a vector array.

But as a word of advice;

  1. Get rid of the magic numbers, http://en.wikipedia.org/wiki/Magic_number_(programming).
  2. Your naming convention, your variable names should as clear as water to understand, "once2" & "frame2 doesn't mean anything in my eyes without a comment or knowing what it's purpose is for. In-fact i can't see personally why you need that last if statement you have. If i could hazard a guess it looks like to be some form of reloading code? As to an example of making your variables clearer, lets take the spacebar boolean you have. Because of your comment, i know that it's changed when the space bar is pressed. To make it clear as crystal however, you can simple change spacebar to something like "hasSpaceBarBeenPressed".

To answer your question:

It seems that you only have 1 instance of your bullet, which gets position at the first time the spacebar bool is true. It seems then that because once2 is incremented and the spacebar value is still equal to true, the only time that your 1 bullet instance you have will return to be re-positioned will be when once2 == 250 which then you reset that once2 value to 0 and then the spacebar bool to false.

share|improve this answer
    
Thanks for your answer :D –  Grey Jan 25 '12 at 16:34
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.