8 Replies Latest reply on Dec 4, 2011 10:41 PM by kglad

    Script not working

    Razvy92

      Hello,

      I'm trying to make a flash piano and add it to my website. I found on the internet some help on this, i used the video tutorial to build the piano (it has 7 octaves and not only 1 as in the vid) then i recorded the key sounds (mp3 files, 44khz, stereo, 64kbps).

      My only problem is that the sounds do not play, the keys are being pressed when clicked. I did everything shown in that tuorial, i even created his piano and everything worked, then i moved on to the bigger project.

      This is the original actionscript i downloaded from that site:

       

      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();
      
      

       

      Here's the one edited by me (there are 100 keys in total so 100 mp3 sounds, each named as 1short,2short and so on. Also the keys are named like key1,key2 and so on):

       

      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);
      
      
      //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=1; i<=100; 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];
      }
      //This statement just calls the setupPiano function to get the piano keys ready to receive mouse clicks
      setupPiano();
      
      

       

      Any help is greatly appreciated. Please bare with me and my lack of knowledge in flash scripts, i do understand how the code works but i am unable to find the error.

       

      Thank you

        • 1. Re: Script not working
          kglad Adobe Community Professional & MVP

          sound names aren't relevant.  you need your sounds to have linkage ids.  do you?

          • 2. Re: Script not working
            Razvy92 Level 1

            i do. every linkage has the same name as the file's name.

            • 3. Re: Script not working
              kglad Adobe Community Professional & MVP

              what do the following traces show:

               

               

              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);
              trace(piano);
              
              //A function to play a note by number.
              function playNote(num:Number){
              trace(num)
                         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=1; i<=100; 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]; } //This statement just calls the setupPiano function to get the piano keys ready to receive mouse clicks setupPiano();

              • 4. Re: Script not working
                Razvy92 Level 1

                didn't help.i added an image which i sliced in photoshop to serve as a piano background to lay the keys on. i put it on the text layer (if you watch the first minutes of the vid you'll see it is the first layer he starts working on). could that affect the whole project? other than that i did not change the project at all.

                 

                later edit:

                i discovered something about my sound files. the ones i downloaded from his tutorial have the channel type set to mono. mine are stereo. can flash handle stereo sounds? there's no error shown by flash related to the script so all i'm thinking of are my sounds. i'll look up for a converter or something, i used fl studio 8 to record them and it doesn't seem to have the channel property when exporting.

                 

                ok so i converted one of my sounds to a mono channel type and also added one of his sounds to the project. it didn't work. once again i'm out of ideas. if it helps i can give you the entire project to take a look at it.

                • 5. Re: Script not working
                  kglad Adobe Community Professional & MVP

                  what do the trace statements show after clicking a few keys?  copy and paste the output.

                  • 6. Re: Script not working
                    Razvy92 Level 1

                    _level0.piano

                    21

                    33

                    47

                    63

                    73

                     

                    That's from my project.

                     

                    _level0.piano

                    62

                    65

                    67

                    71

                     

                    That's from the tutorial project.

                    • 7. Re: Script not working
                      Razvy92 Level 1

                      Damn it i found the problem. In linkage my sounds did not have the .mp3 extension. I took it out from the actionscript:

                      mySound.attachSound(num + "short");

                      And now it works. Can't believe how blind i was...

                      Thanks for all the help, hats off for you

                      • 8. Re: Script not working
                        kglad Adobe Community Professional & MVP

                        you're welcome.

                         

                        p.s.  please mark helpful/correct responses, if there are any.