How can I get multiple bullets on screen?

  • How can I get multiple bullets on screen? Grey

    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:

    // 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 ++;
        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 :( ?

  • 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 (source):

    You could use vectors to create them dynamically.


    #include <vector>
    using namespace std;
    class CBullet
    vector<CBullet *> Bullets;

    Then just use something like:

    void Shoot()
        Bullets.push_back(new CBullet());

    And loop through them:

    void DrawBullets()
        for (int i=0; i<Bullets.size(); i++)
            //do stuff here

    But I guess an array of bullets would work just as fine. Personally, I prefer to use vectors.

    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,
    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.

Related questions and answers
  • ,projectiley,ShootStuff,screen,&lazers[frame]); frame++; projectiley + 1; } I'm trying to get the bullet to fly vertically... and I have no clue how to do that. I've tried messing with the y coordinate but that makes things worse. The laser/bullet just follows the ship :( How would I get it to fire at the starting position and keep going in a vertical line without it following... a total noob when it comes to this so you might have a hard time understanding my code :/ // Position Bullet Function projectilex = x + 17; projectiley = y + -20; if(keystates[SDLK

  • To begin with I am developing a PSP application so I have no clue how to debug a PSP application. That doesn't mean I do not know how debug period. Anyway, when I run the game everything runs fine but when I press the X button on the PSP I see no bullet being drawn or updated on the screen. I know the problem doesn't lie with the pad because I press start and it exits my game so I do not think it is the pad that is causing the problem. Maybe the bullet is being drawn out of the screen already. But I am not sure since I cannot debug maybe somebody can see the flaw in my code. main.cpp class

  • until you pop out into an open space, known as a ' ' (space) character. You lose one life for each INITIAL collision with the X wall, and do not lose another until you have popped out and been freed from it. As an added touch i have made it so after you collide while traveling down the randomly generated map (or rather as the walls move uppward while your character stays put) the X chars you've... and traveling throug it. This is due to some odd reason for the "numb_collisions", which is used to count how many collisions have occured since initial collision (if it's 0 it means it IS the initial

  • I have a very simple effect file shown below. I am using this to draw 2D lines, however it is not behaving how I expected and I can't seem to get my head round why. If I draw a line that goes from 0,0 to 100, 100 for example, I would expect it to draw a line from one corner of the screen to a little way in. I would expect it to treat these numbers as screen coordinates. Instead, the line is huge! A line of about 2 long fills the whole screen. Why is this? How can I modify my shader to 'think' in screen coordinates? // a struct for the vertex shader return value struct VSOut { float4 Col

  • ; //this method is unique to the class player void generate_mesh(); } Now in the game engine I want to have a general object list where I can check for example for collision, move objects, and so... to implement the game engine. So I thought my game engine should control this things: - Moving the objects in the scene - Checking the collisions - Adjusting movements based on collisions - Passing... for all the objects (nextpos = pos + vel) void rotate_obj(); //rotations are the same for every objects too virtual Polygon generate_mesh() = 0; // the polygons are different } and I have 4

  • Language 1.0 Several sites have mentioned that this should be "easy" to do in a GLSL vert shader. But I just can not get it to work. My hunch is that I'm not setting the planes up correctly, or I'm missing... I don't get how coord (a UV vec2 in my mind) is equal to the dot product (a scalar value)? Same problem I had before with "g". What do I set the plane to be? In my opengl c++ 3.0 code, I set... in Maya, have 50k polygons and the modeler is using planer mapping, but Maya will not export the UV's. So I'm trying to figure this out. I've looked at the glTexGen manpage information: g = p1xo

  • I have got some problem in my OpenGL game. I am using bullet physics and I want to achive quite simple effect - I want one object (a sphere) to roll and hit another (box) which will fall down. I have got almost everything but still I have got some errors. When the box is hit it is rotating almost how I would like to. Well, almost means that when ball hits it it falls and rotates however there are about 20 degrees left to the ground. It isn't stoping on a ball because the speed is so high that it is throw away and after a while it "meets" ball This is my code for objects (including floor

  • as drawable and updatable and I get a stack of screens handling my battle graphics. While I haven't been able to get the screen manager to work perfectly yet, I think I can with some time. My...I've been working on a 2d RPG for awhile now, and I've come to realize I've made some bad design decisions. Theres a few things in particular that are causing me problems, so I was wondering what... ago, decided to switch to C++. I wanted to get a good handle on C++ since it's been awhile since I used it heavily, and figured an interesting project like this would be a good motivator. I've been

  • I want to be able to move a sprite from a current location to another based upon where the user clicks in the window. This is the code that I have: #include <SFML/Graphics.hpp> int main...(); } return EXIT_SUCCESS; } But instead of just setting the position I want to use Sprite.Move() and gradually move the sprite from one position to another. The question is how? Later I plan on adding a node system into each map so I can use Dijkstra's algorithm, but I'll still need this for moving between nodes.

Data information