stop(); //Create a sound object and link it to the piano movie clip on the stage var mySound:Sound = new Sound(piano); mySound.setVolume(100); //An array to associate the key code of letters and numbers that are typed with //numbers that correspond to how the piano keys are named (they are named by midi numbers) var codeToKey:Array = new Array(); //black keys played by some of the numbers on the top row of the computer keyboard codeToKey[50] = 61; //2 codeToKey[51] = 63; //3 codeToKey[53] = 66; //5 codeToKey[54] = 68; //6 codeToKey[55] = 70; //7 //white keys played by the top row of letters on the computer keyboard codeToKey[81] = 60; //q codeToKey[87] = 62; //w codeToKey[69] = 64; //e codeToKey[82] = 65; //r codeToKey[84] = 67; //t codeToKey[89] = 69; //y codeToKey[85] = 71; //u codeToKey[73] = 72; //i //A function to play a note by number. function playNote(num:Number){ piano["key"+num]._width = widths[num]-1; piano["key"+num]._height = heights[num]-1; setTimeout(resetSize,250,num); mySound.attachSound(num + "short.mp3"); mySound.start(); } var heights:Array = new Array(); var widths:Array = new Array(); //A function to set up the piano keys to respond to mouse clicks //It also stores the original widths and heights of the piano keys in the arrays above function setupPiano() { for (i=60; i<=72; i++) { piano["key"+i].onRelease = function() { var num:Number = this._name.substring(3, this._name.length) playNote(num); }; heights[i] = piano["key"+i]._height; widths[i] = piano["key"+i]._width; } } //A Function to reset the width and height of a piano key to ts original values function resetSize(num:Number){ piano["key"+num]._height = heights[num]; piano["key"+num]._width = widths[num]; } //Setup a key listener to listen for key presses so the user can type to play notes var keyListener:Object = new Object(); keyListener.onKeyDown = function() { var theCode:Number = Key.getCode(); playNote(codeToKey[theCode]); }; Key.addListener(keyListener); //This statement just calls the setupPiano function to get the piano keys ready to receive mouse clicks setupPiano();