Help with shooting delay

Started by
4 comments, last by nb 16 years, 6 months ago
long start = GetTickCount(); long edelay = 5000; if (start == edelay){ for (int i = 0; i <= ENEMY_COUNT; i ++){ enbullet.y = enemyArray.y + 43; enbullet.x = enemyArray.x + 39; enbullet.y -= enemyArray.movey; } edelay = start + 5000; } I am trying to set this up so that the bullets are brought onscreen every 5 seconds and then moved down the screen. Unfortunetly, nothing happens at all. The bullets are never brought on screen. This code is just a small part of the game loop. Any help is appreciated, thanks in advance.
Advertisement
You'll want to check if start >= edelay, not just equal to. That is assuming that this is ran every frame. Since one frame doesn't last exactly one millisecond (probably lasts longer), it can skip over any integer easily enough.

And when you do this, you should also simply increment edelay by 5000, rather than setting it to start + 5000. This is because, over time, if each time you shoot you're actually at, say, 5010 ms, the gun shots will get off from the original amount. Not a big deal, but it could cause patterns to go wacky after a while.
bump
You might want to take a closer look at the logic.
My guess is that your program never enter the 'if' since GetTickCount returns a number much bigger than 5000.
Ezbez reply got the hints you need.
Here is one way to do it
// GetTickCount returns elapsed milliseconds since system startlong last = GetTickCount();while(!done) // simulation loop, running until 'done' is true{  // enter this if 5000 or more milliseconds has passed since last time  if(last + 5000 <= GetTickCount())   {    <update stuff>    last = GetTickCount(); // update last frame counter  }}


[Edited by - pulpfist on September 30, 2007 7:21:30 AM]
Thank you very much, got it working.
yer you really need to re-visit how GetTickCount() works, but you have it working now so I guess you have already :)

This topic is closed to new replies.

Advertisement