I wrote some animation code a while ago, you're free to use it. You might have to remove the debugging code from it though (
DebugOutput() and
AddTextToConsole() can be removed).
rem -----------------------------------------------------------
rem Animation - Controls animations of objects
rem -----------------------------------------------------------
rem -----------------------------------------------------------
rem Constants
rem -----------------------------------------------------------
rem -----------------------------------------------------------
rem User Defined Types
rem -----------------------------------------------------------
type AnimationVT
logging as integer
CurrentMax 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" , DEBUG_NORMAL )
rem -----------------------------------------------------------
rem global variables
rem -----------------------------------------------------------
global Animation as AnimationVT
rem -----------------------------------------------------------
rem global arrays
rem -----------------------------------------------------------
global dim Animation() as AnimationAT
rem initial values
Animation.CurrentMax = -1
endfunction
function PlayObject( Obj , StartFrame# , EndFrame# , Speed# )
rem logging
if Animation.logging = 1 then AddTextToConsole( 0 , "PlayObject " + str$( Obj ) + "," + str$( StartFrame# ) + "," + str$( EndFrame# ) + "," + str$( Speed# ) )
rem invalid speed
if Speed# = 0.0 then exitfunction
rem local variables
local n as integer
rem find free slot
for n = 0 to Animation.CurrentMax
if Animation( n ).Active < 2 then exit
next n
rem no free slot found, create
if n = Animation.CurrentMax + 1
array insert at bottom Animation()
inc Animation.CurrentMax
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#
if Speed# > 0 then Animation( n ).CurrentFrame# = StartFrame#
if Speed# < 0 then Animation( n ).CurrentFrame# = EndFrame#
Animation( n ).Speed# = Speed#
endfunction
function LoopObject( Obj , StartFrame# , EndFrame# , Speed# )
rem logging
if Animation.logging = 1 then AddTextToConsole( 0 , "LoopObject " + str$( Obj ) + "," + str$( StartFrame# ) + "," + str$( EndFrame# ) + "," + str$( Speed# ) )
rem invalid speed
if Speed# = 0.0 then exitfunction
rem local variables
local n as integer
rem find free slot
for n = 0 to AnimationMax
if Animation(n).Active < 2 then exit
next n
rem no free slot found, create
if n = Animation.CurrentMax + 1
array insert at bottom Animation()
inc Animation.CurrentMax
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#
if Speed# > 0 then Animation( n ).CurrentFrame# = StartFrame#
if Speed# < 0 then Animation( n ).CurrentFrame# = EndFrame#
Animation( n ).Speed# = Speed#
endfunction
function StopObject( Obj )
rem local variables
local n as integer
rem logging
if Animation.logging = 1 then AddTextToConsole( 0 , "StopObject " + str$( Obj ) )
rem find animation index of object
for n = 0 to Animation.CurrentMax
if Animation( n ).Obj = Obj then exit
next n
rem not found
if n = Animation.CurrentMax + 1
if Animation.logging = 1 then AddTextToConsole( 3 , "Object not Active!" )
exitfunction
endif
rem make sure animation is Active
if Animation( n ).Active < 2
if Animation.logging = 1 then AddTextToConsole( 3 , "Animation not Active!" )
exitfunction
endif
rem set frame
set object frame Animation( n ).Obj , Animation( n ).CurrentFrame#
rem stop object
Animation( n ).Active = 0
Animation( n ).Obj = 0
endfunction
function ObjectPlaying( Obj )
rem local variables
local n as integer
rem find animation index of object
for n = 0 to Animation.CurrentMax
if Animation( n ).Obj = Obj then exit
next n
rem not found
if n = Animation.CurrentMax + 1
if Animation.logging = 1 then AddTextToConsole( 3 , "Object not playing!" )
exitfunction 0
endif
rem make sure animation is Active
if Animation( n ).Active < 2
if Animation.logging = 1 then AddTextToConsole( 3 , "Animation " + str$( n ) + " not Active!" )
exitfunction 0
endif
endfunction 1
function ObjectFrame( Obj )
rem local variables
local n as integer
local f# as float
rem find animation index of object
for n = 0 to Animation.CurrentMax
if Animation( n ).Obj = Obj then exit
next n
rem not found
if n = Animation.CurrentMax + 1
if Animation.logging = 1 then AddTextToConsole( 3 , "Object not Active!" )
exitfunction 0.0
endif
rem make sure animation is Active
if Animation( n ).Active = 0
if Animation.logging = 1 then AddTextToConsole( 3 , "Animation " + str$( n ) + " not Active!" )
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 Animation.CurrentMax
if Animation( n ).Active > 1
rem increment frame
inc Animation( n ).CurrentFrame# , Animation( n ).Speed# * TBM.delta#
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
set object frame Animation( n ).Obj , Animation( n ).CurrentFrame#
endif
next n
endfunction
There is a variable in there called
TBM.delta#. That is your timer based movement variable everyone is talking about above. You will have to code that part as well, just use UDTs to define it:
type TMBVT
delta# as float
endtype
global TBM as TBMVT
The code has the following functions:
InitAnimation() - Must be called before using any other functions.
ControlAnimation() - Must be called once every loop.
PlayObject( ObjID , StartFrame# , EndFrame# , Speed# ) - Will play an object's animation.
LoopObject( ObjID , StartFrame# , EndFrame# , Speed# ) - Will loop an object' animation.
StopObject( ObjID ) - Will stop a playing object.
return value = ObjectFrame( ObjID ) - Returns the current animation frame.
return value = ObjectPlaying( ObjID ) - Returns 1 if playing, 0 if not playing.
You MUST ALWAYS call StopObject() BEFORE playing a new animation!!! Otherwise the code will try to play two different animations using the same object, and a rubber bunny with a knife will jump out of your screen and stab you to death
An example:
rem setup screen
sync on
sync rate 60
backdrop on
color backdrop 0
hide mouse
rem load the object
load object "insertmodelhere.x" , 1
rem initialise animation
InitAnimation()
rem main loop
do
rem user info
set cursor 0,0
print "Press space to play animation 1"
print "Press CTRL to play animation 2"
rem play animation 1
if spacekey()
StopObject( 1 )
LoopObject( 1 , 0 , 20 , 1.5 )
endif
rem play animation 2
if controlkey()
StopObject( 1 )
LoopObject( 1 , 20 , 40 , 1.5 )
endif
rem control game elements
ControlAnimation()
rem refresh screen
sync
rem end of main loop
loop
rem end
end
TheComet
Your mod has been erased by a signature, please reduce him [overall] to no larger than 120 kg please.
