1 Reply Latest reply on Nov 24, 2010 4:10 PM by kglad

    Sound

    Bambino1985

      Below is my code for one of my projects and what i need it to do is, on 1 keypress and held down to play sound1 and when i keypress is up to stop playing. Also when 2 keypress are held down it plays sound2 and when keypress is up it stops.

       

      Can anyone help me fill in the gaps, be a big help :-)

       

       

      var global_sound:Sound = new Sound();

       

      onEnterFrame = function(){
         
          // Count how many keys are pressed
          var keyCounter = 0;
          if (Key.isDown( "A".charCodeAt(0) ) ) { keyCounter++;}
          if (Key.isDown( "S".charCodeAt(0) ) ) { keyCounter++;}
          if (Key.isDown( "D".charCodeAt(0) ) ) { keyCounter++;}
          if (Key.isDown( "F".charCodeAt(0) ) ) { keyCounter++;}
          if (Key.isDown( "G".charCodeAt(0) ) ) { keyCounter++;}
          if (Key.isDown( "H".charCodeAt(0) ) ) { keyCounter++;}
          trace(keyCounter);
         
          if (keyCounter == 0){
              // Stop sounds
         
          } else if (keyCounter == 1){
              // Play sound 1
              global_sound.attachSound("sound1.mp3");
              global_sound.start();
         
          } else if (keyCounter == 2){
              // Play sound 2
              global_sound.attachSound("sound2.mp3");
              global_sound.start();
          }

        • 1. Re: Sound
          kglad Adobe Community Professional & MVP

          as long as you follow the same linkage id convention you started with those 2 sounds you can use the following for up to 9 sounds:

           

           

          var tl:MovieClip = this;
          var numSounds:Number = 2;

           

          for (var i:Number = 1; i<=numSounds; i++) {
              tl[s+i]=new Sound();
              tl[s+i].attachSound("sound"+i+".mp3");
          }

           

          var lo:Object = {};
          lo.onKeyDown = function() {
              for (var i:Number = 1; i<=numSounds; i++) {
                  if (Key.getAscii() == 48+i) {
                      tl[s+i].start()
                  }
              }
          };
          lo.onKeyUp = function() {
              for (var i:Number = 1; i<=numSounds; i++) {
                  if (Key.getAscii() == 48+i) {
                      tl[s+i]stop()
                  }
              }
          };
          Key.addListener(lo);