Vectors

Started by
16 comments, last by pundit 15 years, 2 months ago
I think some of the people on here are confused as to what you are trying to use the vector for. If, like Zahlman said, you can explain the problem in detail, then we can be of more help and people will stop giving you confusing information.



FWIW, what I think you're driving at is that you need a list of bullets in your scene, so you can keep track of where the bullets are so you can draw them properly.

In that case, you will need to think about all of the properties of those bullets (position, velocity, texture, etc.) and roll all of those properties up into a single struct.

And that struct will be the type that you specify for your vector.

Using push_back will allow you to add a new bullet to the vector, but it won't make it "shoot". You have to do that yourself. That means that each frame, before you render anything, you need to iterate through that vector and update the position of each bullet based on its velocity. And update anything else you need to as well.

Then, when it comes time to draw each bullet, iterate through the list again. Call the SDL blit function once per bullet. For each call, the function will expect a destination rect which describes the position of the bullet on the screen, and you can glean this information from your bullet struct.


Advertisement
O Ok. Sorry, I was a bit unclear.

I can already shoot. But my simple rig only allows me to have one of that type of bullet on the screen at once and I can only shoot another when the other bullet is >= Screen_Width and Screen_Height.
How does the code currently work? Do you just have some global values for the bullet's position, velocity, surface, etc.?
Umm, it's a mess of bools and if statments. Very unorganized. I do a have a different copy that only uses one bool and more clear functions, but that one doese not shoot. Something wrong with the logic. Id have to take a look.

But yes, it just have variables for the X and Y offsets then X and Y velocity.
Have you worked with structs before?
No.
Well, structs are a great organizational tool, so I would look into that. They are very easy to learn. In fact, if you are using SDL to draw this stuff, then you probably have already used them (The SDL_BlitSurface requires you to use SDL_Rect, which is a struct). So, you likely already have an intuitive understanding of them, and I think once you learn about them, the solution to your problem will become evident.
"Now when you say push back another bullet. I dont understand how that shoots it. So if it is being pointed at the functions, every value in the vector has the member functions already applied to it? And when you say right parameters, what do you mean?"

well, you "shoot" because you just created a new bullet on the vector,
so now that it exists in the vector and you update it's position each time you call the update function, one could say the bullet has been fired and is moving.
With the right parameters i mean stuff like it's starting position x and y coordinates, and maybe speed.

So you could write a fire function which takes parameters, for example:

void Fire(int x,int y)
{
m_BulletVector.push_back(Bullet(x,y) //i pass on the starting coordinates as parameters for the Bullet element

}
------------------------------------------------------------------------
Now for this vector you could write the following "loop" :

for ( vector<Bullet>::iterator it = m_BulletVector.begin() ; it != m_BulletVector.end(); it++ )
{
it->Update();
it->Paint();
}
------------------------------------------------------------------------
Your class Bullet should look something like this:

class Bullet
{
Bullet(int x,int y):m_X(x),m_Y(y) //fill in its starting position
{} //this is your constructor
//don't forget a destructor
void Update() //make bullet move 1 to the right every time it gets updated
{
m_X++;
}
void Paint() //draw the bullet
{
//drawing code goes here
}

};

This topic is closed to new replies.

Advertisement