• Advertisement
Sign in to follow this  

How to make my character shoot more then once

This topic is 3573 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am making a 2D sidescrolling game in C++ using SDL. I want to make my player be able to shoot more than one bullet at a time. Right now when he shoots for the second time, the first bullet just dissapears. I tried doing SDL Rect playerfirebox[ 3 ]; and then put everywhere else playerfirebox.x[ shot_number ] and playerfirebox.y [shot_number] And then increased shot_number with every key press. I was told I need to use a vector? If thats true can you point me to a really good( easy to understand ) tutorial. Thank You

Share this post


Link to post
Share on other sites
Advertisement
hehe, first person i have ever helped but i do now the answer to this one. To get more than one bullet you will need to use some sort of array such as a vector. This vector will store the position of all the bullets, you can then update the vector every frame and delete the bullets that are no longer relevant. Thats all i can tell you, to much of a noob myself to give any further advice.

Share this post


Link to post
Share on other sites
pseduoish code:

vector<Bullet> bullets;

// Game loop
for(;;)
{
// Input
if (key_fire)
bullets.push_back( new Bullet( source, direction ) );

/* Handle other inputs */


// Update
for (Iterator it = bullets.begin; it != bullets.end; it++)
{
Bullet b = *it;
b.move( timedelta );

if ( b.collide( enemy ) )
{
enemy.damage( really_painfully );
bullets.erase( it );
}
}

/* Update other objects */


// Draw
for (Iterator it = bullets.begin; it != bullets.end; it++)
(*it).render( screen );

/* Draw other objects */
}



//Edit:
Just noticed you also wanted help on using vectors. If you don't already know about them, I think it's safe assume that you're a beginner with C++, so the best advice for you would be to pick up a proper book on C++ and start learning. Any good book on C++ should teach to basics of the standard library.

If you want a free one, Thinking in C++ is free to read online.


[Edited by - Luctus on July 11, 2008 8:09:17 PM]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement