4 Replies Latest reply on Aug 25, 2007 9:14 PM by kglad

    loadSound and OnLoad not working in a class

    DJ Gecko Level 1
      Hi, I'm using AS2.

      I can't get an external .mp3 file to play after it's finished loading.

      Here's my code:

      class Sounds {
      var musicPlay:Boolean;
      var mainsong:Sound;
      //
      function Sounds () {
      musicPlay = true;

      }
      //
      public function Theme () {
      trace ("Sound Theme");
      //create a new Sound object
      //var mainsong:Sound = new Sound ();
      // if the sound loads, play it; if not, trace failure loading
      mainsong = new Sound ();
      mainsong.onLoad = function (success:Boolean) {
      trace ("theme loading : " + success);
      if (success) {
      trace ("theme loaded");
      if (musicPlay) {
      trace ("theme play");
      MusicToggle ("play");
      }
      }
      };
      // load the sound
      mainsong.loadSound ("sound/ice_hopper.mp3",false);
      }

      //
      public function MusicToggle (thisToggle:String):Void {
      trace ("Music Toggle");
      if (thisToggle == "play") {
      mainsong.setVolume (50);
      mainsong.start (0,99999);
      musicPlay = true;
      } else if (thisToggle == "stop") {
      mainsong.stop ();
      musicPlay = false;
      }
      }
      }

      I am able to start and stop the .mp3 when a button calls the MusicToggle function.

      Thanks!
        • 1. Re: loadSound and OnLoad not working in a class
          kglad Adobe Community Professional & MVP
          you're out of scope in that onLoad method. so, you're not able to access the MusicToggle() method.

          try:

          • 2. Re: loadSound and OnLoad not working in a class
            DJ Gecko Level 1
            Thanks!

            I got it to work by adding var self = this;
            Code is below. Which approach is best do you think?

            Thanks!

            class Sounds {
            var musicPlay:Boolean;
            var mainsong:Sound;
            //
            function Sounds () {
            musicPlay = true;

            }
            //
            public function ThemeLoad () {
            trace ("Sound Theme");
            // self is used due to the onLoad being it's own object and so unable to acces class instance's methods and properties
            var self = this;
            //create a new Sound object
            mainsong = new Sound ();
            // if the sound loads, play it; if not, trace failure loading
            mainsong.onLoad = function (success:Boolean) {
            trace ("theme loading : " + success);
            if (success) {
            trace ("theme loaded");
            if (self.musicPlay) {
            trace ("theme play");
            self.MusicToggle ("play");
            }
            }
            };
            // load the sound
            mainsong.loadSound ("sound/jet_ice_hopper.mp3",false);
            }
            //
            public function MusicToggle (thisToggle:String):Void {
            trace ("Music Toggle");
            if (thisToggle == "play") {
            mainsong.setVolume (50);
            mainsong.start (0,99999);
            musicPlay = true;
            } else if (thisToggle == "stop") {
            mainsong.stop ();
            musicPlay = false;
            }
            }
            //
            public function PlaySFX (sfxToPlay:String):Void {
            trace("PlaySFX: "+sfxToPlay);
            }
            }
            • 3. Re: loadSound and OnLoad not working in a class
              DJ Gecko Level 1
              Oops, or OOP, I just moved the function onSoundLoad to its own method within the class, and now I think your method is the best ; )

              Thanks again!
              • 4. Re: loadSound and OnLoad not working in a class
                kglad Adobe Community Professional & MVP
                you're welcome.