0

Hi, I have a rock object. Right now all it does is fall from the sky. I made 10 of them and put it in a std::vector<rocks*> rock_v. The rocks have random x position but start at the same height.
I generate the rocks like this:

for(int i = 0; i < 10; i++)
{
    rock_v->init();
}

The problem with this is all the rocks will spawn at the same time (millisecond apart from each other). How can I make it so they fall at a certain interval -- say 0.5 sec.

2
Contributors
2
Replies
16
Views
2 Years
Discussion Span
Last Post by HuePig
0

This code is only viable for C++11, but nothing prior to that...

std::chrono::milliseconds timespan(111605); // or however long you want to wait in milliseconds

std::this_thread::sleep_for(timespan);

If your compiler does not allow C++11 functions then you will have to use a platform dependant method which is not advisable. e.g. For Windows you would say:

#include <windows.h>

for(int i = 0; i < 10; i++){
    rock_v-.init();
    sleep(100);  //milliseconds
}

for unix:

#include <unistd.h>

for(int i = 0; i < 10; i++){
    rock_v->init();
    sleep(100);  //in milliseconds
}
0

thanks for the advise. I just figured that SFML have a clock.
I used that clock insted.. heres the pseudo code:

time = clock.getelapsedtime();

if(time >= 500) //millisecond
{
    //spawn rocks
    //restart clock
}
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.