I can't see the error in this.
The purpose of this piece of code is to have my game wait the given number of seconds before running the Fade2Black function. I''m not trying to stall the timer in a while loop to control animation speed, but just passively time something. When I run this, however, it ignores the time I specify in LongEnough and runs Fade2Black() immediatly. Any ideas why?
DWORD startTime; //start time variable
static int check = 0;
if (check == 0) //we only want to set startTime once
{
startTime = timeGetTime(); //start counting here
check = 1;
}
DWORD currentTime = timeGetTime(); //check elapsed time
DWORD longEnough = 3000;
//if enough time has gone by, do our fade
if ((currentTime-startTime) > longEnough)
{
Fade2Black("slow");
}
I assume it''s in a game loop which updates every frame and checks if it is long enough. What i noticed is that the starttime variable is being reset every iteration. Make starttime static and it should work.
[Edited by - kmsixpence on October 17, 2005 7:59:15 PM]
[Edited by - kmsixpence on October 17, 2005 7:59:15 PM]
Starttime is set only once, since "check" is static. But your problem is that after 3 seconds, the screen will fade to black every single frame. You need to reset StartTime when you fade to black.
~CGameProgrammer( );
~CGameProgrammer( );
start time get's set once. but then when the loop repeats it loses it's value. cgameprogrammer also has a good point that it fill keep going and you need to reset starttime
[Edited by - kmsixpence on October 17, 2005 7:28:25 PM]
[Edited by - kmsixpence on October 17, 2005 7:28:25 PM]
I appreciate the suggestions, but neither of them are the problem. StartTime needs to be set only once, because after the given amount of time I want Fade2Black() to be run each time through the game loop.
The problem I''m having, is that it doesn''t wait those 3000 milliseconds at all. It just immediatly goes into the Fade2Black(). If I reset StartTime, it only slows down the rate Fade2Black works. It is the first 3 seconds that it seems to ignore.
The problem I''m having, is that it doesn''t wait those 3000 milliseconds at all. It just immediatly goes into the Fade2Black(). If I reset StartTime, it only slows down the rate Fade2Black works. It is the first 3 seconds that it seems to ignore.
Yes, check is static, so the code inside the if statement will only be exectured once, but startime is NOT static, and the variable is local, so each time the code is called after the first, start time will have a bogus value, and any behaviour is undefined
like, kmsixpence said, make starttime static and it should work, and you want to make sure that fadetoblack is only called once, or knows how to deal with being called multiple times.
--michael
like, kmsixpence said, make starttime static and it should work, and you want to make sure that fadetoblack is only called once, or knows how to deal with being called multiple times.
--michael
Ah, good point. I was thinking starttime was global, but if it isn''t then you do need to make it static.
~CGameProgrammer( );
~CGameProgrammer( );
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement