How do Slow 2D Animations Sprites Down?

Blitz3D Forums/Blitz3D Beginners Area/How do Slow 2D Animations Sprites Down?

Hotshot2005(Posted 2011) [#1]
I got a problem as My 2D Sprites Animations is going too fast and I want to slow down in acceptable animations rate.

I used the code like this

Frames=Frames+1
IF Frames>=9 then Frames=1

I am not sure if this code allow to slow down like this

If MilliSecs() > tmrFrm + 10 Then
tmrFrm=MilliSecs() ; 'reset' the timer
Frames=( Frames + 1 ) Mod 3
EndIf

or it is

Vwait?

I just not sure hence why I ask questions.


semar(Posted 2011) [#2]
You could use float increments to increase the frame value. For example, you could use this snippet in your game loop:

;out of loop:
frame# = 1.0
frameSpeed# = 0.2

;Inside loop
frame = (frame + frameSpeed) mod 8
if frame = 0 then frame = 1

Play with frameSpeed values until you get the desired frame speed :)

Another way is to use Millisecs(), you can find an example in the on-line manual: http://www.blitzbasic.com/b3ddocs/command.php?name=LoadAnimImage&ref=2d_cat

Regards,
Sergio.

Last edited 2011


Matty(Posted 2011) [#3]
maximum_frames = 9

if abs(millisecs()-frametime)>100 ;100 millisecs for each frame to remain on
	frame=((frame+1) mod maximum_frames) 
	frametime=millisecs()
endif




GfK(Posted 2011) [#4]
I use Sergio's method.


Matthew Smith(Posted 2011) [#5]
Yeah - I use the same as Sergio also


jfk EO-11110(Posted 2011) [#6]
Maybe best mix the two suggestions:

speed#=0.2

ms=millisecs()-17; must initialize

while game ; start mainloop
old_ms=ms
ms=millisecs()
frametime=ms-old_ms
delta#=frametime/16.667
...
frames#=frames#+(delta#*speed#)
if frames#>max_frames# then frames#=1
...


So this is going to run with the same animation speed on any machine, regardless of the screens framerate.

An other solution would be to use the timer for some sort of sequencing, as you already suggested:

ms=milisecs()
if ms>ani_timer
frames=frames+1: if frames> max_frames then frames=1
ani_timer=ms+100
endif

This will allow you to organize things with some sort of timeline.

The first sample at the other hand offers delta (although here probably not really correct, since normaly delta isn't frametime/17, but 17/frametime!). Delta is very useful for Updateworld, because it will make your animated 3D meshes be in sync with the users individual screen framerate (and machine speed).

So you'd do this:

delta1#=frametime/16.667
delta2#=16.667/frametime
...
frames#=frames#+(delta1#*speed#)
if frames#>max_frames# then frames#=1
...

if walking then moveentity player,0,0,walkspeed# * delta2#

updateworld(delta2#)
renderworld()


Last edited 2011