Kaisuo,
I think you should use a different method to animate your sprite.
The timing technique could be good as well, but is affected by the time passed; more, it uses Millisecs() and a bunch of other statements, which would consume resources which could be used in other departments instead.
With no offense to anyone who have suggested you the time animation here, I would use a simple method, which I try to
describe now.
So you have your animated image, with some animation inside; that means, for example, that you have the walking animation which goes from frame 12 to frame 22; the standing animation which goes from frame 0 to frame 11; and so on.
So, basically, if we had a generic function which would play an arbitrary animation between a given start and end frame, we have done our task, right ?
That said, we need our f_start and f_end global variables, which stay for frame start and frame end. And we need a global variable where to store the current frame: f_current
Then we need a function that loops between the two values, and resets to the starting one once reached the end frame.
A rough example could be:
Function update_frame()
f_current = f_current + 1 ;add one frame
if f_current > f_end then ;check if it is out of bounder
f_current = f_start ;reset to start frame
endif
end function
Then, in your main loop, you can have:
.
.
update_frame() ;this call will update our global variable f_current
drawimage sprite_image, x, y, f_current
This code should work flawlessy. When you need another animation to be played - ex. standing animation - you just need to:
- change the start and end frame values;
- set the current frame to the new start frame value.
For example, if you want that your sprite animation change from walking (12-22) to standing (0-11), then you just have to do:
f_start = 0
f_end = 11
f_current = f_start
Experiment now. Then read further.
The method above works fine, but has a disavantage, that is, there's no control about the animation speed; the function will cycle through the frames, but there's at the moment no way to speed up or speed down the animation loop.
Well, there are more than one way to solve this problem, and without the use of any timer or Millisecs() and neither time elapsed counter tricks.
For example.
You can decide to declare your f_current variable as float. In this way, you can use fractional value to increment the frames, so to have more control on the animation speed.
In other words, let's take a look at the frame increment statement in the function above:
f_current = f_current + 1 ;add one frame
What if we do something like:
f_current = f_current + 0.1
You see ? Your f_current variable - that should be declared as float in this case - will be slowly incremented, and you can vary the increment value as you like: 0.5, 0.001,2.5... you have an endless choice here, welcome in the world of Real Numbers !
The code should be slight changed, however, to be able to handle float values instead of integers. First of all, let's change our global declarations, and also add a new variable which can be used to change our animatiion speed dinamically:
Global f_current# ;the # says that it's a float variable
Global f_speed# ;the animation speed
;let's assign some initial value. You don't need the # sign anymore, once the variable has been declared.
f_current = 0
f_speed = 0.5
Now let's change our update_frame function:
Function update_frame()
f_current = f_current + f_speed ;update f_current
;we use the Floor function here, to cast the float value of f_current to an integer value. This is needed, otherwise our animation will never use the last frame properly.
If Floor(f_current) > f_end then ;check if it is out of bounder
f_current = f_start ;reset to start frame
endif
end function
And our main loop:
update_frame() ;this call will update our global variable
f_current
drawimage sprite_image, x, y, Floor(f_current) ;note the
Floor function. It will take the integer part of our float
f_current variable
Now, if you want to change the animation, as well as the animation speed, you know already what to do...
More, you could make the whole thing even better, by using a type to store all the data (start and end frame, speed,..) for each animation, and pick the appropriate item each time you need to change the current animation to another one.
Hope this has sense for you,
Sergio.