If I were the confident sort of NoOb,
I'd post this in CODE SNIPPETS, but I ain't.
So since I am a NoOb, and not so confident...
I thought I'd present my solution to a specific
problem I encountered. It may be VERY specific to only me,
and therefore useless to others.
My situation is that I have numerous animated sprites on
screen at the same time. "Play Sprite" has proven unhelpful
for my specific tasks.
The sprites I wish to animate all have different frame counts.
But I require them to all be in sync. ( yes, reworking them to
all have the same frame count would help, but that's not the
point of my post )
I'd also prefer that my animations not appear to be loops.
For my specific purposes I am creating an animated shore line.
The tide washes in, and then back out... And shouldn't appear to
a constant loop.
As such I have written the following code,
which seems to work, and of course could be improved upon.
global moment as float
global smooth = 20
global current as float
function setMoment()
max as float
max = (screen fps()*smooth)+1
randomize_timer()
current = current + rnd(smooth)+1
if current > max then current = 0
moment = current/max
endfunction
The above snippet of course is just that "a snippet"
it doesn't "DO" much of anything... What it does do
is set the global variable MOMENT.
Which in my case is then applied ( as a percentage multiplier )
to the maxNumberOfFrames for each sprite. All are then in sync
to the "MOMENT"...
I am not sure how to demo the code effectively, but here goes...
global moment as float
global smooth = 10
global current as float
load image "sprite01_frame00.img",s `unique number
`... `sprite "01" has five frames
load image "sprite01_frame04.img",uq+4 `unique number plus 4
load image "image02_frame00.img",s
`... `sprite "02" has twelve frames
load image "sprite01_frame11.img",uq+11 `unique number plus 11
do
setMoment()
for s = 1 to 2
if sprite exist(s) then delete sprite s
m = maxFramesForCurrentSprite() `a fucntion to return the maxFrames
currentImageForSprite = int(m*moment) `all images sync to moment
sprite s,Xpos,Ypos,currentImageForSprite
next s
loop
function setMoment()
max as float
max = (screen fps()*smooth)+1
randomize timer()
current = current + rnd(smooth)+1
if current > max then current = 0
moment = current/max
endfunction
The above code REALLY does nothing, it can't even be executed.
But hopefully you understand how it works...
It is using the variable MOMENT to decide which frame to use.
All sprites no matter how many frames long, execute in order
and ~should~ appear smooth, despite sync/frame rate. They also
~should not~ appear to be in a perfect loop, because of the
random increment of the moment.
Adjustments to the SMOOTH variable will make changes to
the animation. The lower the SMOOTH variable, the more
variation ( random ) to the loop. The higher the SMOOTH
variable the smoother the animation ( but less random the
loop ).
SMOOTH set to "0" ( zero ), is of course useless.
SMOOTH set to one is the most random least smooth version
of the animations.
SMOOTH set to the maxNumberOfFramesOfTheLongestAnimation
should make the sprite with the max number of frames seem
very natural, all other sprites should vary based on the
above description. They would appear more 'loopish'
depending on how few frames are available.
Playing with the variable SMOOTH, should allow you to
find a happy medium.
I cannot tell you how happy I am with the implementation,
but at the same time, I am certain my explaination is far
from perfect. So questions are welcome.
If there any veterans that can share a better way to do it...
Well, any and all input is more than welcome.
============ : Windows 200 SP4+, 3.2GHz Hyperthreaded
-Dejunai ............: 2GB RAM, nVidia 6600 256MB AGP 8x
============ : AC'97 basic A-Bit Motherboard Audio ( drivers always Maintained )