New Object every 15 seconds

Hello everyone!
thank you in advance for your help.

I am starting my first game in C++ using the Allegro library. I have created an object which I want to auto-create itself every 15 secs. I have an array:

char * flights[] = {"IBE6041","AVA010","BAW0454","ELY068"};

then instatiate the class:

1
2
3
4
5
int i = 0;
Flight flights[i];
flights[i].configurardata(1000, 305);
p = flights[i].getx();
q = flights[i].gety();


there are also a couple of functions that determine the y and x position in order to draw a little aircraft moving around, but the above code is basically what I need to auto-reproduce every 15 secs. ... I would like more aircrafts to appear every 15 seconds but I can't manage to make that happen. I can write the code to make more than one appear at the same time, but I want a new airplane appear while the project is running so that the user will do stuff with them.

Any ideas of how to go about, or what type of function, code I need?
thanks again!
1
2
int i = 0;
Flight flights[i];


I don't know what you think this is doing, but it's not legal C++.

If your compiler is allowing it (by some compiler extension), then what it's doing is creating a zero-length array (ie: you don't have any Flights -- the 'flights' array is empty and useless).

Furthermore, this is confusing because your 'Flight' flights array has the same name as your 'char*' flights array. I would rename the char* array to "flightnames" or something. Also, it should be a const char*, not a char*.


What you want is some kind of container. Possibly a vector:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//  create a resizable array of flights:
std::vector<Flight> myflights;


// when you want to create a new flight:
myflights.push_back( Flight() );
// this will increment the size of the array by 1, creating a new flight at the end of your array


// when you want to know how many flights there are:
int number_of_flights = myflights.size();


// when you want to access individual flights, you can do so the same way you would if you had
//  an array... with the [] operator:

// get the first flight:
myflights[0].getx();  // etc 
Thank you.

I realize now that the name leads to confusion and will take care of that. I will read about vectors and try to do it the way you recomend to see if I get better results. The i=0 was there cause I wanted to loop through the array and create a new Object each time I did an i++.

I had in mind creating originally an array (you recomend myflights) of lets say 200, with the flight names in it, then loop through the array and create an object every 15 secs (with the i++) draw them on the screen so that people acted on each flight.. My problem is obviously that the i=0 affects the array and also that I don't know how to loop through the array to draw one at a time at an interval of time...

thanks again for your help... I will put myself to work :)
Topic archived. No new replies allowed.