Animation

Interactive states

A character that waves when you click it, a menu that opens when the pointer comes over it, a pet that eats when you feed it: the States panel makes a movie clip, or the whole movie, answer clicks and keys without writing any code. Each pose or bit of animation is a state, and arrows between the states say what sends the playhead from one to another.

The States panel: three boxes, idle, eat and sleep, with a green arrow into idle; arrows from idle to eat marked feedBtn is clicked, from eat to idle marked it finishes, and between idle and sleep marked sleepy is on (at its end) and sleepy is off
A pet's states: it bobs (idle) until Feed is clicked, eats once, then bobs again; it sleeps while the sleepy switch is on.

Before you start

States are made of two things you already know from the timeline:

The states can belong to the main timeline (the scene) or to a movie clip symbol: open the symbol by double-clicking it, and the panel shows that symbol's states, so every copy of it on the stage behaves the same way.

Open the States panel

Choose Window › States. The panel opens beside the Timeline and shows the states of the timeline you are editing. The graph is on the left, what you have picked in the middle, and the inputs and listeners on the right.

States

Click Add State.... The Label list offers the frame labels that are not states yet; pick one. Or type a new name and give the frame where it starts: Phoenix makes the label for you, on a layer called States.

The Add State dialog: Label, At frame and It, which is set to loops
A new label made at frame 37.

Each state does one of four things with its frames:

ItWhat happens
loopsPlays its frames over and over, like a walk cycle or an idle bob.
plays oncePlays its frames and stops on the last one: a jump, a wave, a door opening.
holds its first frameStays still on its first frame: a button that is pressed, a light that is off.
follows a numberShows the frame a number input picks, and keeps up as the number changes: a gauge, a dial, a health bar, a face from sad to happy. Input chooses the number and the values at the state's first and last frame; between them, its frames are spread evenly.

Click a state's box to change it. Name is its frame label: renaming the state renames the label. Through lets a state play on through the frames of later labels too. The movie starts in this state marks the starting state, shown with a green arrow; the first state you add starts out as that. Drag a box to move it about the graph.

Layers of states

A movie clip on the timeline can have states of its own that play at the same time as the timeline's: a character walks while its face changes expression, a button glows while its icon spins. Give the movie clip an instance name, then click Add Layer..., name the layer and choose the movie clip. The Layer list above the graph switches between the timeline's own states and each layer's.

The States panel showing the Face layer: the state mood, which follows the number happy from 0 to 10 over frames 1 to 5, and blink, which plays once; a key B goes to blink and its end goes back to mood
The face's own states: its mood follows happy, and B makes it blink.

A layer's states are the movie clip's frame labels, so label the clip's frames first; Add State... offers them. Its transitions join its own states, and the Any state box sends it to one of them whatever it is doing. Every layer goes by the same inputs and listeners, so one key or click can move several at once. Remove Layer takes out the layer and its states; the clip's labels stay.

Transitions

A transition is an arrow from one state to another. To make one, drag from the small dot on the right edge of a state's box and let go on another box. (Or pick a state and click Add Transition.) Click an arrow, or the words on it, to say when it happens:

The States panel with a transition picked: From idle, To eat, When Something is clicked, What feedBtn with a Use Selected button, Only if no input, and Wait for its animation to finish first
From idle to eat, when the button named feedBtn is clicked.
WhenThe transition happens
Something is clickedWhen the instance named in What is clicked: pressed and let go over it.
Something is pressedAs soon as the mouse button goes down on it.
The mouse button is let go over somethingWhen the button comes up over it, wherever it went down.
The pointer moves onto something, or off itWhen the pointer comes over the instance, or leaves it: for hover effects.
A key is pressedWhen the Key picked is pressed: a letter, a number, Space, Enter, an arrow key, or any key.
Its animation finishesWhen the state reaches its last frame (for a loop, each time round).
Some time goes byAfter the number of seconds given in the state.
Only when an input is setAs soon as the input in Only if has the value given (see Inputs).

For a click, What lists the named buttons and movie clips on the timeline; you can also select one on the stage and click Use Selected. Only if adds a condition to any transition: feedBtn is clicked, if hungry is on. Wait for its animation to finish first lets the state play to its end before going, so a bob or a step is never cut off halfway.

The Any state box at the top is for a transition that leaves whatever state is playing: drag from its dot to a state, for example to send a character to hurt whatever it is doing when it is hit.

Inputs

An input is a named value the transitions can go by. Click Add Input, then name it in the middle column. There are three kinds:

Listeners

A listener sets an input when something is clicked or a key is pressed, whatever state is playing. Click Add Listener and choose When (a click, press, release, hover or key), What or Key, the Input, and Then: turn on, turn off or flip a switch, add to a number or set it, or fire a trigger. The list reads like a sentence: When napBtn is clicked: turn on sleepy.

Listeners and transitions together make small games: a listener adds 1 to score each time a coin is clicked, and a transition goes to win when score is at least 10.

Bindings

A binding shows an input on the stage, and keeps it up to date as the input changes. Click Add Binding, choose the Input, the instance it is Shown in (a movie clip, or a dynamic text field with an instance name; select one on the stage and click Use Selected), and what it sets As its:

As itsWhat it does
textThe text field shows the input's value: a score, a count of lives.
visibilityThe movie clip shows while a switch is on (or a number is not 0), and hides otherwise.
frame, x, y, turn, fade (%), size (%)A number sets it in proportion: Input from 0 to 10 sets it from 20 to 320 moves the clip from x 20 at 0 to x 320 at 10, held at the ends. A health bar, a needle, a fade.

A movie needs no states for bindings: inputs, listeners and bindings alone make a counter, a slider or a scoreboard.

Pins: layouts that fit the window

A published page can fill the browser window, whatever its size. Pins say where each part goes as the window grows and shrinks: a toolbar along the top, a menu bar along the bottom, a logo in a corner, a title in the middle. Click Add Pin, choose the instance (a movie clip or text field with an instance name on the main timeline), and tick the edges it Keeps to:

The stage you draw on is the layout at its first size: everything starts where you put it. With pins, the published page shows the movie at its own scale rather than scaling the whole stage to fit, so the window's extra room goes where the pins say. Test Movie shows the stage at its own size.

Scrolling

A long list, a gallery or a page of text can scroll inside a box. Click Add Scrolling, choose the movie clip, and set its Box: how wide and tall a part of it shows, from where it is placed on the stage. Scrolls sets the way (down, across or both), and Glide how much of its speed it keeps each frame once let go.

When the movie plays, drag the clip to scroll it, or flick it and let go to send it gliding; the mouse wheel scrolls it too. Past either end it goes only half as far, as if held back by a spring, and once let go it springs back to the end. The rest of the clip is hidden outside the box.

Try it

Click Test at the top of the panel. The movie plays in Test Movie, where you can click and press keys and watch the states change.

Each change in the panel is one step that Edit › Undo takes back. Remove takes out the state, arrow, input or listener picked; a state's frame label stays on the timeline.

On a web page

A movie published as an HTML5 page lets the page around it read and set its inputs, so a form, a button of the page's own or live data can drive the animation. phoenix.start gives the page the player:

phoenix.start({container: "phoenix-stage"}).then(function (player) {
    player.setInput("score", 3);        // bindings show it, transitions may go
    player.fire("poke");                // a trigger
    console.log(player.input("score"), player.state);
    player.onChange(function (now) {    // whenever the state or an input changes
        console.log(now.state, now.inputs);
    });
});

inputs() gives all of them at once. The movie's own scripts see the same values.

What Phoenix writes

You never need to look, but the states are turned into ordinary ActionScript: Phoenix writes it on the first frame of the States layer, between two comment lines that start with // [States] and // [/States]. It is ActionScript 3 for an ActionScript 3 document, and ActionScript 1 and 2 otherwise, as the document's publish settings say. Because it is plain ActionScript, the states work in Test Movie, in a published SWF or HTML5 page, and in any other app that opens the file, with nothing of Phoenix's needed.

Learn it step by step

The lesson A pet that answers clicks (Help › Tutorials...) makes a little pet eat when Feed is clicked and sleep when Nap is clicked, with an input, two listeners and four transitions.