My brother recently approached me with an interesting project: He wanted to make a flash movie that would have enable someone to control the mouth movements of a "bobblehead" with keystrokes.

Although I have not done this before, I was certain it would be something fairly easy to do.  ActionScript 3's "Keyboard" class has a plethora of functions and methods that allow one to do just about anything with a keyboard and a flash animation.

So here's the breakdown:  The animation needed to have five "states":  Default, profile, wobble, "ooh" and talk; and each needed to be assigned to a separate key.  

Cake.

The first step was to create the background movie.  My brother did this in Motion 3, and we set it inside a Movie Clip on the first frame of the flash movie.  Next, we defined another movie clip that contained all of the movements for the head "states".  

Beyond that, all that was left to do was to define the keyboard actions.  The following is the totality of the code:

function doTheBobble(event:KeyboardEvent):void {
    if(event.keyCode == Keyboard.SPACE) {
        Mouth.gotoAndPlay(2);
    }
    if(event.keyCode == Keyboard.LEFT) {
        Mouth.gotoAndPlay(21);
    }
    if(event.keyCode == Keyboard.RIGHT) {
        Mouth.gotoAndPlay(31);
    }
    if(event.keyCode == Keyboard.UP) {
        Mouth.gotoAndPlay(41);
    }
    if(event.keyCode == Keyboard.DOWN) {
        Mouth.gotoAndPlay(51);
    }
}
stage.addEventListener(KeyboardEvent.KEY_DOWN, doTheBobbleHead);

It's very simple.  To begin from the bottom, an eventListener is added to the "stage" (the flash animation).  When the KeyboardEvent "KEY_DOWN" is "heard", the function is fired.

The function is also simple.  There is a series of "if" statements that determined which key has been pressed.  If a particular key is pressed, the Flash movie clip instance "Mouth" is fired at the specified frame.

So there it is.  A pretty cool little application for very little coding. 

Here's the movie.