Here's how I've been doing it with all of my projects:
rem -----------------------------------------------------------
rem Animation - Controls animations of objects
rem by TheComet
rem -----------------------------------------------------------
rem -----------------------------------------------------------
rem Constants
rem -----------------------------------------------------------
#constant AnimationMax 100
rem -----------------------------------------------------------
rem User Defined Types
rem -----------------------------------------------------------
type AnimationVT
logging as integer
endtype
type AnimationAT
active as integer
CurrentFrame# as float
StartFrame# as float
EndFrame# as float
Speed# as float
Obj as word
endtype
rem -----------------------------------------------------------
rem Initialise
rem -----------------------------------------------------------
function InitAnimation()
rem debug
DebugOutput(0 , "Initialising Animation, max="+str$(AnimationMax))
rem -----------------------------------------------------------
rem global variables
rem -----------------------------------------------------------
global Animation as AnimationVT
rem -----------------------------------------------------------
rem global arrays
rem -----------------------------------------------------------
global dim Animation(AnimationMax) as AnimationAT
endfunction
function PlayObject(Obj , StartFrame# , EndFrame# , Speed#)
rem local variables
local n as integer
rem logging
if Animation.logging = 1 then AddTextToConsole("PlayObject "+str$(Obj)+", "+str$(StartFrame#)+", "+str$(EndFrame#)+", "+str$(Speed#) , 0xFFFFFFFF , 1 , 0)
rem find free slot
for n = 0 to AnimationMax
if Animation(n).active = 0 then exit
next n
rem no free slot found
if n = AnimationMax + 1
if Animation.logging = 1 then AddTextToConsole("No free slot!" , 0xFFFF0000 , 2 , 0)
exitfunction
endif
rem set frame
set object frame Obj , StartFrame#
rem set values
Animation(n).active = 2
Animation(n).Obj = Obj
Animation(n).StartFrame# = StartFrame#
Animation(n).EndFrame# = EndFrame#
Animation(n).CurrentFrame# = StartFrame#
Animation(n).Speed# = Speed#
endfunction
function LoopObject(Obj , StartFrame# , EndFrame# , Speed#)
rem local variables
local n as integer
rem logging
if Animation.logging = 1 then AddTextToConsole("LoopObject "+str$(Obj)+", "+str$(StartFrame#)+", "+str$(EndFrame#)+", "+str$(Speed#) , 0xFFFFFFFF , 1 , 0)
rem find free slot
for n = 0 to AnimationMax
if Animation(n).active = 0 then exit
next n
rem no free slot found
if n = AnimationMax + 1
if Animation.logging = 1 then AddTextToConsole("No free slot!" , 0xFFFF0000 , 2 , 0)
exitfunction
endif
rem set frame
set object frame Obj , StartFrame#
rem set values
Animation(n).active = 3
Animation(n).Obj = Obj
Animation(n).StartFrame# = StartFrame#
Animation(n).EndFrame# = EndFrame#
Animation(n).CurrentFrame# = StartFrame#
Animation(n).Speed# = Speed#
endfunction
function StopObject(Obj)
rem local variables
local n as integer
rem logging
if Animation.logging = 1 then AddTextToConsole("StopObject "+str$(Obj) , 0xFFFFFFFF , 1 , 0)
rem find animation index of object
for n = 0 to AnimationMax
if Animation(n).Obj = Obj then exit
next n
rem not found
if n = AnimationMax + 1
if Animation.logging = 1 then AddTextToConsole("Object not active!" , 0xFFFF0000 , 2 , 0)
exitfunction
endif
rem make sure animation is active
if Animation(n).active = 0
if Animation.logging = 1 then AddTextToConsole("Animation "+str$(n)+" not active!" , 0xFFFF0000 , 2 , 0)
exitfunction
endif
rem set frame
if object exist(Animation(n).Obj)
set object frame Animation(n).Obj , Animation(n).CurrentFrame#
endif
rem stop object
Animation(n).active = 0
Animation(n).Obj = 0
endfunction
function ObjectPlaying(Obj)
rem local variables
local n as integer
rem logging
if Animation.logging = 1 then AddTextToConsole("ObjectPlaying "+str$(Obj) , 0xFFFFFFFF , 1 , 0)
rem find animation index of object
for n = 0 to AnimationMax
if Animation(n).Obj = Obj then exit
next n
rem not found
if n = AnimationMax + 1
if Animation.logging = 1 then AddTextToConsole("Object not playing!" , 0xFFFF0000 , 2 , 0)
exitfunction 0
endif
rem make sure animation is active
if Animation(n).active = 0
if Animation.logging = 1 then AddTextToConsole("Animation "+str$(n)+" not active!" , 0xFFFF0000 , 2 , 0)
exitfunction 0
endif
rem playing or not
if Animation(n).active = 1 then exitfunction 0
endfunction 1
function ObjectFrame(Obj)
rem local variables
local n as integer
local f# as float
rem logging
if Animation.logging = 1 then AddTextToConsole("ObjectFrame "+str$(Obj) , 0xFFFFFFFF , 1 , 0)
rem find animation index of object
for n = 0 to AnimationMax
if Animation(n).Obj = Obj then exit
next n
rem not found
if n = AnimationMax + 1
if Animation.logging = 1 then AddTextToConsole("Object not active!" , 0xFFFF0000 , 2 , 0)
exitfunction 0.0
endif
rem make sure animation is active
if Animation(n).active = 0
if Animation.logging = 1 then AddTextToConsole("Animation "+str$(n)+" not active!" , 0xFFFF0000 , 2 , 0)
exitfunction 0.0
endif
rem return value
f# = Animation(n).CurrentFrame#
endfunction f#
function ControlAnimation()
rem local variables
local n as integer
rem loop through all active animations
for n = 0 to AnimationMax
if Animation(n).active > 1
rem increment frame
inc Animation(n).CurrentFrame# , Animation(n).Speed#
rem play object
if Animation(n).active = 2
rem animation reaches end
if Animation(n).CurrentFrame# > Animation(n).EndFrame#
Animation(n).CurrentFrame# = Animation(n).EndFrame#
Animation(n).active = 1
endif
if Animation(n).CurrentFrame# < Animation(n).StartFrame#
Animation(n).CurrentFrame# = Animation(n).StartFrame#
Animation(n).active = 1
endif
endif
rem loop object
if Animation(n).active = 3
rem animation reaches end
if Animation(n).CurrentFrame# > Animation(n).EndFrame#
Animation(n).CurrentFrame# = Animation(n).StartFrame#
endif
if Animation(n).CurrentFrame# < Animation(n).StartFrame#
Animation(n).CurrentFrame# = Animation(n).EndFrame#
endif
endif
rem update object frame
if object exist(Animation(n).Obj)
set object frame Animation(n).Obj , Animation(n).CurrentFrame#
else
StopObject(Animation(n).Obj)
endif
endif
next n
endfunction
Call
InitAnimation() before your main loop and put
ControlAnimation() in your main loop.
I should probably add this to my library of functions for you guys.
[EDIT] Just noticed that there are some functions in the code specifically for my project. You'll have to comment them out for this to work.
TheComet