Back

Animation

Reading

Timers

Remember that processing runs at 60 fps. This means that an integer, incrementing every frame will hit a multiple of 60 every 1 second.

60 (1s), 120 (2s), 180 (3s), ...

You may also use the global reserved variable frameCount which is an integer representing the amount of frames since your sketch has started.

Resetting Timers

Let's assume you want to loop 3 images, each for 1 second and then back to the first.

Using FrameCount

Using another variable as a timer also works, provided you keep track of the current frame you want to show.

Character State

You're going to have to maintain the state of your character, and at some point it will change.

For example, your character will be either: idle, moving, firing / action, ...

To keep this as simple as possible use a single integer to keep track and leave a comment for yourself.

Character state

Frame Management

There are 2 options for managing a list of frames for your character, an Array or an ArrayList. Since your character will most likely not add or remove any animation frames during your game, an ArrayList is not necessary.

For every animation of your character you will need to store a list of frames for that animation. We'll use arrays.

Arrays of Frames

Changing Frames

Discussion

Notice how we are looping our frame at a rate of 60 / 5 which means 12 frames per second, which is pretty good for animation.

Also notice how we must check the state our character is in, is the character walking? shooting? or just standing there.

Review switch statements in the processing documentation! Default will execute if no cases were matched.

Storing Frames

Make sure not to be loading the same frames for multiple instances of the same object. This will most likely incur a performance penalty.

Storing all arrays of frames inside an Animation class would be the preferred technique. Then to access those frames it's simply a matter of calling
animation.sequence[framePosition]

Store animation frame sequences in a single class and load them all in the constructor.
Plan out your character states and frame arrays carefully.
When updating frames, make sure you're updating the right list and checking the limit of your frame Array and not exceeding it.
Watch out for an index out of bounds error, this means your currentFrame variable is trying to access a frame in one of your arrays that is not there. This usually means the currentFrame variable is too large or you are updating the wrong list.

Other Techniques

Not all animation requires frames, for example the arms of a clock rotate based on the time.

Regardless of the style of animation you decide to use, timers will be important in managing that animation.

Timers can be based on a custom integer, or reserver global variables like frameCount and millis()

Rotation Example