# Running a function for a period of seconds

This is a discussion on Running a function for a period of seconds within the C++ Programming forums, part of the General Programming Boards category; Well, I wanna run a function for a fixed period of seconds, like 5 seconds or so, how do I ...

1. ## Running a function for a period of seconds

Well, I wanna run a function for a fixed period of seconds, like 5 seconds or so, how do I go about doing this?
I've looked through ctime reference, and I dunno which of those functions to use for this, or how :S

2. Does a single run of the function take more than 5 seconds?

3. 5 seconds was just some number that got in my head, I'm trying to make the character jump, and I need it to go upwards for a short period of time

4. Ah, so not really a specific function then.

Just part of a normal game loop which
do
- detects if there is any key press
- performs actions on the key press
- moves NPC characters
- redraws the screen
- works out alive-or-dead

Except now it would be
do
- detects if there is any key press
- performs actions on the key press
- perform any player auto movements (such as jump)
- moves NPC characters
- redraws the screen
- works out alive-or-dead

At the start of a jump, you determine a trajectory (didn't you ask about gravity earlier?). The player character continues along this trajectory (one screen refresh at a time) until something interesting happens (say landing on another floor, grabbing a rope, dying horribly etc).

5. ok, I kinda got it working, but not well enough xP
Now the player just spawns up in the air and falls down D:

Code:
void character::goUp(float dt){

float tempY = y;

do{
y -= gravityPull + 100 * dt;

}while(tempY <= y + 200);
}
how can I avoid this? xP

6. Originally Posted by Akkernight
ok, I kinda got it working, but not well enough xP
Now the player just spawns up in the air and falls down D:

Code:
void character::goUp(float dt){

float tempY = y;

do{
y -= gravityPull + 100 * dt;

}while(tempY <= y + 200);
}
how can I avoid this? xP
Well, look at what it does -- it moves y down by 300 units, I guess. If you want to draw the person going up (I believe in your system y going down is player going up?) then you can't do it all in one function, since the drawCharacter doesn't get a chance to act until this function returns.

Popular pages Recent additions