Implementing Attack-Speed( Delay )- help please :-)

Started by
19 comments, last by rakoon2 19 years, 8 months ago
This seems to work! :-)

const int delay = player->getDelay_bow();    frameTick = (SDL_GetTicks()/1000) - Ticks;    if( delaycounter <= 0 )    {       //fire       delaycounter = delay;     }     if( delaycounter > 0 )      delaycounter -= frameTick;    Ticks = frameTick;


but it doesn't work in the first ~5secs!
Advertisement
Make sure delay counter is set to zero, also that when the game starts, Tick is the same as frameTick.

Glad you got it working! [grin]
Hmm! ok it isn't working! I shoot faster and faster..! I don't change the delay! BUt it still gets faster and faster over time. :/
Here is my code:

1.: Variable declaration:
 int delaycounter = 0; int frameTick = 0; Uint32 Ticks = 0;  int fire_arrows = 0;



2.: update delay
    frameTick = (SDL_GetTicks()/1000) - Ticks;    if( delaycounter > 0 )      delaycounter -= frameTick;          Ticks = frameTick;


3.: check if key is pressed
  // if attack key is pressed:  if( delaycounter <= 0 )  {      fire_arrows++;      const int delay = player->getDelay_bow();      delaycounter = delay;   }


4.: spawn arrow
 and in the end:   if( fire_arrows > 0 )  {    //spawn error object    fire_arrows--;  }

5.: go to 1.



Thank you! :/
Right, first off - don't scale frameTicks by 1000 if you're not scaling delay by 1000. If you scale it by 1000, you want to use floats as the delayCounter and frameTick.


In level_init() or whatever, you need to set up your variables.
 float delaycounter = 0; float frameTick = 0; float Ticks = frameTick;  int fire_arrows = 0;



In game_tick() you need to calculate the tick and update the delay

 // calc tick count frameTick = (float)(SDL_GetTicks() / 1000) - Ticks; if( delaycounter > 0 ) {   delaycounter -= frameTick; }      Ticks = frameTick;  // check keypresses  // if attack key is pressed:  if( delaycounter <= 0 )  {      // delay counter is at 0, can fire      delaycounter = player->getDelay_bow();   }  


Do not set tick, frametick, etc to zero after the frame is updated. They need to persist through the game.

Why are you incrementing fire_arrows? What does that variable signify? As far as i can tell, you're either a) Setting the trametick, tick, etc to zero on each frame or the problem has something to do with how you're using fire_arrows.

I really can't tell any more from here. [shrug]
here is a screenshot, what I get if I wait a little.
I shoot them faster and faster!
http://members.chello.at/ennemoser/time_bug.jpg

Here is the code related to it:
float delaycounter = 0;float frameTick = 0;float Ticks = frameTick;while( done == false ){   //....   frameTick = static_cast<float>( (SDL_GetTicks() / 1000) )  -   Ticks;   if( delaycounter > 0 )     delaycounter -= frameTick;         Ticks = frameTick; if( attack_key_Is_Pressed == true ) {   if( delaycounter <= 0 )   {           const int delay = player->getDelay_bow();           delaycounter = delay;           // now spawn the arrow           // (add to object-list)    } }    //.... }   



Help please!:/ Thank you very very much!
This is how I would do it:

float delaycounter = 0;float frameTick = 0;float Ticks = frameTick;while( done == false ){   //....   frameTick = static_cast<float>( SDL_GetTicks() ) / 1000.0f   -   Ticks;   delaycounter += frameTick;         Ticks += frameTick; if( attack_key_Is_Pressed == true ) {   if( delaycounter >= static_cast<float>(player->getDelay_bow()) ) // be consistant on casts   {                      delaycounter = 0;           // now spawn the arrow           // (add to object-list)    } }    //.... }


[EDIT] I spotted a pair of important bugs in the timer code!
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Oh, thank you. But that deosn't work too! I shoot more and more arrows over time! Why?

http://members.chello.at/ennemoser/time_bug.jpg


This one doesn't even let me spawn an arrow! ( nothing happens if I press the key )
frameTick = ( static_cast<float>( SDL_GetTicks() ) / 1000.0f )  -   Ticks;   delaycounter += frameTick;   Ticks += frameTick;//..if( key_is_pressed == true ){   if( delaycounter >= static_cast<float>(player->getDelay_bow()) )   {         // spawn arrow         delaycounter = 0;    }}//..



Thank you..! :3




:edit:
I just logged the delaycounter to a logfile.log every frame:
LOG FILE         contains errors/infos from the last game-start0.1590.2330.2760.3130.3490.3840.4220.4570.4930.5280.5620.5980.6380.6810.7190.7570.7940.8290.8650.9040.940.9761.011.0471.0971.1311.1691.2071.2411.2771.3131.3471.3841.421.4541.4891.5271.5651.6011.6371.6751.7111.7491.7841.821.8551.891.9271.9662.0022.0372.0752.1082.1442.1812.222.2552.2912.3282.3642.4212.4662.4912.5172.5432.5682.5932.6192.6452.672.6952.7262.7532.7812.8082.8412.8712.8982.9282.9552.9853.0133.0413.0683.0973.1253.1533.1813.2113.2443.2813.3083.3363.3643.3883.4143.4413.4693.4963.5243.5513.5753.6013.6273.6523.6963.7293.7553.7833.8143.9544.1114.1964.2344.2594.2924.3264.3544.3884.4174.5084.5334.564.5874.6154.6454.674.6974.7294.7594.7914.8194.8484.8764.9054.9344.9624.9915.0185.0495.0855.1165.1465.1745.2075.2395.2685.2975.3225.3485.3735.3985.4275.4555.4835.5165.5455.5745.6025.635.6565.6845.715.7385.7645.7925.8195.8485.8765.9015.9385.9776.0136.0476.0786.1076.1366.1636.1936.2236.2546.286.3056.3336.3586.3896.4176.4436.4766.5066.5336.5686.5956.6236.6486.6746.7016.7296.7546.7796.8056.836.8576.8826.9076.9336.9586.9857.0117.0387.0657.0937.1197.1537.1847.2177.2457.277.2987.3257.3537.3817.4127.4437.4697.4987.5267.5527.5797.6187.6477.6757.7027.7317.7657.7957.8227.8517.8827.9117.9387.9637.9928.0188.0488.0758.1028.1298.168.1928.2618.3018.3338.359//..

this seems to low.. I will never reach the delay_bow in this way!
ohhk.. I found the bug in the Thunder_Hawk/my/our last attempt

Ticks += frameTick;should beTicks = frameTick;


But I still have the bug that let the arrows spawn faster and faster: http://members.chello.at/ennemoser/time_bug.jpg
Why? :/
You said that the delay was somewhere between 40 and 75, right? Those units are obviously in milliseconds, but here they are being compared to seconds. In other words, you will have to wait between 40 and 75 seconds between bow firings. A quick fix would be to change this line:
if( delaycounter >= static_cast<float>(player->getDelay_bow()) ) // be consistant on casts


to:

if( delaycounter >= static_cast<float>(player->getDelay_bow())/1000.0f ) // be consistant on casts


Also, I neglected this is my first post, but if you're using my way , you have to initialize delayCounter to the bow's delay.

float delaycounter = static_cast<float>(player->getDelay_bow())/1000.0f;float frameTick = 0;float Ticks = frameTick;
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________
Quote:Original post by rakoon2
ohhk.. I found the bug in the Thunder_Hawk/my/our last attempt

*** Source Snippet Removed ***

But I still have the bug that let the arrows spawn faster and faster: http://members.chello.at/ennemoser/time_bug.jpg
Why? :/


Don't do that!! That introduces the arrows spawning faster and faster bug. Just read my previous post.
______________________________________________________________________________________The Phoenix shall arise from the ashes... ThunderHawk -- ¦þ"So. Any n00bs need some pointers? I have a std::vector<n00b*> right here..." - ZahlmanMySite | Forum FAQ | File Formats______________________________________________________________________________________

This topic is closed to new replies.

Advertisement