Game Development Reference
In-Depth Information
animation should run. These two properties should be added to the class's
read/write/clone methods as well to ensure that they're saved properly.
The
time
and
increaseFrames
are runtime properties only:
private int maxImages = 1;
private float animationFps = 10f;
private float time = 0f;
private int increaseFrames;
3. Now, let's go to our
update
method. This is the method that runs once
every frame. We add functionality to check whether it's time to change
the frame in the particle or not. The logic goes like this: when the current
passed time is larger than the time between frames, increase the frame in-
dex by one. Using a
while
loop rather than an
if
statement allows us to
compensate for low frame rate, by skipping several frames, if necessary,
to keep up with the frames per second:
public void update(float tpf){
super.update(tpf);
float timeBetweenFrames = 1f / animationFps;
time += tpf;
increaseFrames = 0;
while (time > timeBetweenFrames){
increaseFrames++;
time -= interval;
}
}
4. In
influenceRealtime
, which is the method that is run once per
particle and frame, all we do is tell it to increase the
imageIndex
value
if needed, making sure not to exceed the maximum images in the cycle:
public void influenceRealtime(Particle particle,
float tpf) {
super.influenceRealtime(particle, tpf);
if(increaseFrames > 0){
particle.imageIndex = (particle.imageIndex +
increaseFrames) % maxImages;
}
}