Public Group

# Help with vectors

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

## Recommended Posts

I need Bullets.Screenx to equel Players

.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.Screenx = Players

.x ; Bullets.Screeny = Players

.y ; }

##### 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.Screenx = Players

.x;
Bullets.Screeny = Players

.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 on other sites
No that is not really what I want.

What I want is for the player to shoot the bullet from his location.

How would you do that?

##### 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 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 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]

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 14
• 13
• 9
• 12
• 9
• ### Forum Statistics

• Total Topics
631438
• Total Posts
3000073
×