Elapsed Time and Delay Time problem

I’m working on a horse racing game. Each time the game starts a random horse wins because of the intervals I randomly add to their speed . I ve no problem until here. My question is about the sprites seen per second. I ve four sprites for each horse and using sourceRect and DestRect method I draw them. So, the horses all are running in the game:) and one of them wins. But frame shown per second , I cant adjust it. Below , I just copied the codes I used for the fifth horse: (the same codes also for other horses like 1,2,3,4)

    Texture2D horse5;

float elapsed5;
float delay5 = 100f;
int frames5 = 0;

Rectangle destRect5;
Rectangle sourceRect5;

 destRect5 = new Rectangle(500, 435, 100, 100);

horse5 = Content.Load(“horse5”);

elapsed5 += (float)gameTime.ElapsedGameTime.TotalMilliseconds;

            if (elapsed5 >= delay5)
            {
                if (frames5 >= 3)
                {
                    frames5 = 0;
                }
                else
                {
                    frames5++;
                }
                elapsed5 = 0;
            }

            sourceRect5 = new Rectangle(100 * frames, 0, 100, 100);

       spriteBatch.Draw(horse5, destRect5, sourceRect5, Color.White);

The other horses delay times are all the same. So, things seem ok. But when I want one of the horses run slower or faster ( i mean the frame rate), that I change the delay=100f to delay=1f or delay=5000f , nothing changes. Still all the horses frame speeds are same. How can I control this?

float elapsed4;
float delay4 = 100f;
int frames4 = 0;

float elapsed3;
float delay3 = 100f;
int frames3 = 0;

float elapsed2;
float delay2 = 100f;
int frames2 = 0;

float elapsed1;
float delay1 = 100f;
int frames1 = 0;

If the code you posted is how you use it, it has multiple problems.
Most obvious one is that you calculate value for variable frames5 as a position in the animation.
Afterwards to calculate sourceRect5 you use variable called frames instead. If this is some not initialized variable it will always be zero which results in using always first frame.

Edit: Man I totally missunderstood your question. My bad. :smiley:

But I used this code for some other games too which worked pretty well. and I have no problems here either. All four frames come one after another. My problem is the speed of showing one frame after another. also I ve no problems about the movements.

Finnhax, thanks for the reply. I solve that problem like this ;

speedofhorse5 = (int)(rand.Next(-1, 2));

destRect5.X = destRect5.X + speedofhorse5;

so the movement from X axis is in control. They race, and each time a different one wins because of random adding.

the runners. each frame comes after another. so you see him as if running. if I change this runner’s frame rate, the time interval passing from one frame to another, all of other runners frame rates change too. and the five runners always seem to run in same speed. ( I think I cant tell what I mean)

when I change delay of one horse from 100f to 1f , all the others frame rates change too. Why I cant decide different frame rates?

I think that when displaying the frame you are calculating the source rect with this part:
sourceRect5 = new Rectangle(100 * frames, 0, 100, 100);
For this sourceRect it should be:
sourceRect5 = new Rectangle(100 * frames5, 0, 100, 100);

From what you posted I don’t see anything else that would make the animation be independent of the delayN variables.

1 Like

perfect. janikve thank you very much. Now I see that its a small mistake of me. Perfectly right. Now I adjust the fps. I forgot to write frames1, frames2…

1 Like