i need to make more than one thing of the same thing. that thing is a ball. so when the user presses space bar it makes a new ball on the screen. i think i can do this with a set of classes maybe. thanks for the help.
class Ball
{
// ...
};
// somewhere
std::vector< Ball > myballs; // array of balls
// called when space is pressed
void AddBall()
{
myballs.push_back( Ball() );
}
Note it's probably not most efficient to store the Ball in the vector as per the above example. A better alternative might be to use a std::deque instead so there's less chance of a bulk copy. Or dynamically allocate the balls with new (although that makes cleanup more difficult).
Both vectors and deques provide thus a very similar interface and can be used for similar purposes, but internally both work in quite different ways: While vectors are very similar to a plain array that grows by reallocating all of its elements in a unique block when its capacity is exhausted, the elements of a deques can be divided in several chunks of storage, with the class keeping all this information and providing a uniform access to the elements. Therefore, deques are a little more complex internally, but this generally allows them to grow more efficiently than the vectors with their capacity managed automatically, specially in large sequences, because massive reallocations are avoided.
when i run that balls[1].positionx = 1; it randomly closes. i am sure its this part because i commented it out and the program exicuted fine. this is the exact code.
I'm guessing you're stepping out of bounds in your array. Don't do balls[1] unless you have at least 2 balls in the vector. If you have less than 2, you're overflowing the buffer (bad!)