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.

Recommended Answers

All 2 Replies

Member Avatar for Search_not

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
}

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
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.