5 Replies Latest reply on Dec 31, 2010 12:18 PM by kglad

    Play a sound once if hitTest is true?

    prodigalmaster Level 2

      Hi, I have 2 movieclips with instance names: bonus and platform. (and a movieclip inside platform with instance name: hit)

      I need a sound to play when hitTest is true.

      Here is non-working code:

      var snd50Points:Sound = new Sound();
      snd50Points.attachSound("snd50Points");
      snd50PLoop.onEnterFrame = sound50Points2;
      function sound50Points2() {
       if (bonus.hitTest(platform.hit)) {
        trace("hit");
        this.start(0,0);
        delete this.onEnterFrame;
       }
      }
      snd50Points.onSoundComplete = function() {
       snd50PLoop.onEnterFrame = sound50Points2;
      };


      I dont see why this doesnt work. The sound is in the library with linkage name: snd50Points

      The trace("hit"); doesnt give an output when they do collide.

      I have a seperate hitTest that does give an output however, could this mean I cant have a hitTest of the same thing at once? I cant delete the onEnterFrame loops of the other hitTest because it has code that needs repeating.

       

      So yeah, I need a sound to play once when they collide and play the sound once again when they collide and so on...

      Thanks for reading and hopefully answering.

        • 1. Re: Play a sound once if hitTest is true?
          kglad Adobe Community Professional & MVP

          what's in your output panel when you use:

           


          var snd50Points:Sound = new Sound();
          snd50Points.attachSound("snd50Points");
          snd50PLoop.onEnterFrame = sound50Points2;
          function sound50Points2() {
          trace(bonus+" "+platform+" "+platform.hit);
          if (bonus.hitTest(platform.hit)) {   trace("hit");   this.start(0,0);   delete this.onEnterFrame; } } snd50Points.onSoundComplete = function() { snd50PLoop.onEnterFrame = sound50Points2; };

           

          • 2. Re: Play a sound once if hitTest is true?
            prodigalmaster Level 2

            nothing.

             

            I decided to comment out that code and restart completelly, here i have:

            var bonusGet = false;
            var snd50 = new Sound();
            snd50.attachSound("snd50Points");
            snd50.onSoundComplete = function() {
             bonusGet = false;
            };
            if (bonusGet == true) {
             snd50.start(0,0);
            }
            snd50.onSound
            

             

            bonus.onEnterFrame = function() {
            if (this.hitTest(platform.hit)) {
              bonusGet = true;
            }
            //more code...
            }
            

             

            If I put the

             if (bonusGet == true) {
             snd50.start(0,0);
            }

            in an onEnterFrame it obviously plays the sound repeatedly in a blur. However I need it to play once!

            • 3. Re: Play a sound once if hitTest is true?
              kglad Adobe Community Professional & MVP

              your first problem was caused by failing to have a movieclip with instance name snd50PLoop at the time that code executed.

               

              create that movieclip and retest.

              1 person found this helpful
              • 4. Re: Play a sound once if hitTest is true?
                prodigalmaster Level 2

                ah well, thanks This new code ive just edited works.(I think!)

                ...with a named onEnterFrame

                loop which I delete just after the if statement is

                true and then I remake the onEnterFrame when the bonus respawns.

                 

                However it slightly changes the behaviour of a movieclip completelly unrelated... but it all works.

                (I have two blocks one on top of the other, you move the bottom block up then the top block moves perfectly with it but now the top block kinda sinks slightly into the bootom block while its moving. Not a gameplay problem though.

                 

                Thanks for the help

                 

                var bonusGet = false;
                var snd50 = new Sound();
                snd50.attachSound("snd50Points");
                snd50.onSoundComplete = function() {
                 bonusGet = false;
                };
                snd50Loop1.onEnterFrame = snd50Loop();
                function snd50Loop() {
                 if (bonusGet == true) {
                  snd50.start(0,0);
                  delete snd50Loop1.onEnterFrame.snd50Loop;
                 }
                }
                platform.onEnterFrame = function(){
                if (this.hitTest(platform.hit)) {
                  bonusGet = true;
                };
                
                bonus.onEnterFrame = function(){
                  if (this._y == -25) {
                onEnterFrame = snd50Loop();
                }
                 };

                Yeah well I tested it a lot and sometimes (rarely it plays the sound twice...)

                • 5. Re: Play a sound once if hitTest is true?
                  kglad Adobe Community Professional & MVP

                  you're welcome.  but this would be better:

                   

                  var snd50Points:Sound = new Sound();
                  snd50Points.attachSound("snd50Points");
                  var snd50Loop:MovieClip=this.createEmptyMovieClip("snd50Loop",this.getNextHighestDepth());
                  snd50PLoop.onEnterFrame = sound50Points2;
                  function sound50Points2() {
                  if (bonus.hitTest(platform.hit)) {
                    trace("hit");
                    snd50Points.start(0,0);
                    delete this.onEnterFrame;
                  }
                  }
                  snd50Points.onSoundComplete = function() {
                  snd50PLoop.onEnterFrame = sound50Points2;
                  };