Sign in to follow this  
kingpinzs

Help with vectors

Recommended Posts

I need Bullets[i].Screenx to equel Players[P].x ect. but when i try to do that the bullet stays with player and does not keep moving. How do I get the bullet to start at player and then move on its own? So were do I declare this at? for (int i =0; i <Bullets.size(); i++) { Bullets[i].Screenx = Players[P].x ; Bullets[i].Screeny = Players[P].y ; }

Share this post


Link to post
Share on other sites
Nowhere. Your piece of code moves all bullets so they are at the player position.

The code you must write is:

Bullets[i].Screenx = Players[P].x;
Bullets[i].Screeny = Players[P].y;

This must be written in the part of your code where you generate a new bullet.

It is assumed above that P is the index of the player that shoots the bullet, and i is the index of a bullet that is not currently in an "used" state, that is, a bullet that you don't mind disappearing somewhere else to appear on the player.

Share this post


Link to post
Share on other sites
When you first shoot a bullet you need to set it's initial position to that of the player that fired it, then in your update section of your game loop you loop through any bullets currently on screen and adjust their X or Y position according to the direction that bullet is travelling.

Share this post


Link to post
Share on other sites
right that is what I thought but were would I put that at?

I need to know were to put bullet.x = player.x gor only new bullets. I cant figure out how to tell the programe that they are new bullets and need to start at playerx.

Share this post


Link to post
Share on other sites
You program dosn't really need to know which bullet is new and which is not. When you create a bullet you set it's initial position, and then in your update section you update each bullet with it's direction and speed.

When you fire a bullet you might call a function FireBullet() which assign the player position and direction to your bullet and you can also assign a variable holding the bullet speed. Every tick you update the bullet and calculate, from the current position, it's new position using the diretion and speed.

I'm not sure if you're using std::vector but if you are it's simply to use .push_back and .erase when you fire/hit something (a std::list would be better for ever-changing bullets though [smile]).
If you're not using STL, you could make an array of pointers pointing on bullets. When you hit something you delete the bullet. When you're updating you simply check if there is a valid pointer and if so, update it.
I guess you could also make an array of already instanciated bullets and add a bool to the bullet struct/class. That bool would then be true if a bullet exist and false if it is not. When you create you set it to true and assign all values. When it hit something you set it to false. Now when updating you just see if that bool is true, and if so update the bullet.

I hope this help a little, and that i understood your question [smile]

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this