Create and manipulate objects?

I'm stuck with creating objects on the fly. I want to do something like:
1
2
3
4
5
6
7
8
9
10
11
12
13
class projectile {
...
}

int counter = 0;
if(leftclick) {
  projectile projx; //Where x is the counter variable
  counter++;
}

for(int i = 0; i < counter; i++) {
  proji.move(); //Again, I want to replace i with a variable number
}


No idea how to do that.

The best thing I could come up with is:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class projectile {
  bool active; //Constructor defaults to false
  ...
}

projectile projectiles[1001];

if(leftclick) {
  for(int i = 0; i < 1000; i++) {
    if(projectiles[i].active == true) {
      continue;
    }
    else {
      projectiles[i].active = true;
    }
}

for(int i = 0; i < 1000; i++) {
  projectiles[i].move();
}


There has got to be a better way...
Last edited on
Use an std::vector of projectiles.
Topic archived. No new replies allowed.