Timing Problem

BlitzMax Forums/BlitzMax Beginners Area/Timing Problem

Emmett(Posted 2005) [#1]
I have a flag flapping in the breeze. It has 10 frames in the animation. Right now it increments 1 frame per pass through the main loop.
Whoa nelly - It looks like there is a hurricane a blowin!

I would like the flag to switch frames 1 time each 3 or 4 passes through the main loop.

There can be no delays in the main loop so using Delay or CreateTimer will not work.

What is the best way to overcome this problem?

(edit) I figured out a way to do it but would welcome any better ideas.
If frame=9 frame=0
	If flt=>1
	DrawImage flag,280,10,frame			'Note: This routine keeps the flag from flapping too fast.
	EndIf								'Variable flt (flagTimer) gets incremented at the bottom
	If flt=>3							'of the Repeat/Until loop.
	DrawImage flag,280,10,frame
	frame:+1 flt=0
	EndIf


Thanks


GW(Posted 2005) [#2]
psudo code:
get the current time with millisecs()
if (last_time_a_frame_changed - now) > How_long_to_wait then
advance_animation
last_time_a_frame_changed = now
endif


{cYan|de}(Posted 2005) [#3]
or just use a float and inc it slower;P


LarsG(Posted 2005) [#4]
Just a quick code example..:
Const flag_frames = 9

Repeat
	Do_Flag(100)
	FlushMem
Until KeyHit(KEY_ESCAPE)

End

Function Do_Flag(rate:Int)
	Local newtime:Int
	Global oldtime:Int, frame:Int
	newtime = MilliSecs()
	If (newtime - oldtime) > rate
		frame:+1
		If frame > flag_frames Then frame = 0
		oldtime = newtime
	EndIf
	DrawImage flag,280,10,frame
EndFunction



z80jim(Posted 2005) [#5]
I'm sure Lars has the right idea here. I'm working on an Ultima IV tile game and there are lots of tiles with this kind of animation. The way Lars suggested is what I ended up doing. The big advantage is that it gives you a consistent animation rate independent of the speed of the machine or your program etc.


Booticus(Posted 2005) [#6]
Dude! Rad Ultima was my favorite RPG of all time! You gonna post a worklog?


Emmett(Posted 2005) [#7]
@LarsG - Excellent - Thank You
I plugged it right in and now control of the flag waving is very precise.
@z80jim - Yes, Big Advantage, consistent and independent of speed of machine - very important in cases like this.

I spent a good deal of time studying that code.

Only one question remains:
Why is :int used? It works (apparently) the same without that.


LarsG(Posted 2005) [#8]
The default type is :int(eger), but I just put it in there for consistency.. :)
No need to write it if you don't care.. :p