4 Replies Latest reply on Feb 19, 2007 3:07 PM by Rothrock

    Putting sound in an Analogue Clock

    geilezeit
      My analogue clock is working but i don't know how to add the sound to it - a tick for each second. The sound file is called "tock".
      Thanks for any tips. Below is the script, so far.

      // Frame 1
      attachMovie("hourHand", "newhourHand",1);
      attachMovie("minuteHand", "newminuteHand",2);
      attachMovie("secondHand", "newsecondHand",3);
      newhourhand._x=ani_clock_mc._x;
      newhourhand._y=ani_clock_mc._y;
      newminutehand._x=ani_clock_mc._x;
      newminutehand._y=ani_clock_mc._y;
      newsecondhand._x=ani_clock_mc._x;
      newsecondhand._y=ani_clock_mc._y;
      function updateClock(){
      var time= new Date();
      var hour=time.getHours();
      var minute=time.getMinutes();
      var second=time.getSeconds();
      newhourHand._rotation=(hour*360)/12
      newminuteHand._rotation=(minute*360)/60
      newsecondHand._rotation=(second*360)/60
      }

      // Frame 2
      updateClock();

      // Frame 3 ("tick" is the frame 2 label)
      gotoAndPlay("tick");

        • 1. Re: Putting sound in an Analogue Clock
          geilezeit Level 1
          Any ideas on this one?
          • 2. Re: Putting sound in an Analogue Clock
            Rothrock Level 5
            What have you tried since yesterday?

            I'm sure you've looked up the Sound class in the Actionscript language reference. There you carefully read every entry for that class.

            So you decided, yup, I need an instance of a sound object.

            So you made the following code outside of the updateClock function, because you don't need to create it afresh every time.

            var myTock:Sound=new Sound(this)

            You then attack the sound since it is in your Library and has the linkage set to Tock. (Make sure you have checked export for actionscript.) I'm guessing this sound is a fairly small file so it is fine to include it as part of the swf and not have to externally stream or load it.

            So you can attach it to your sound object like such. Again outside of the updateClock function because once attached it stays attached and doesn't need to be done over and over.

            myTock.attachSound("Tock");

            Finally, finally to play the sound all you need to do is put the following code in the updateClock function.

            myTock.start();

            That should just about be it.
            • 3. Re: Putting sound in an Analogue Clock
              geilezeit Level 1
              Thanks for your reply. I tried that and I get sound but it's like a machine gun. Instead of one tick a second I suppose it's going 6 a second.
              This is my revised script.

              // Frame 1
              var myTock:Sound=new Sound(this)
              myTock.attachSound("tock");
              attachMovie("hourHand", "newhourHand",1);
              attachMovie("minuteHand", "newminuteHand",2);
              attachMovie("secondHand", "newsecondHand",3);
              newhourhand._x=ani_clock_mc._x;
              newhourhand._y=ani_clock_mc._y;
              newminutehand._x=ani_clock_mc._x;
              newminutehand._y=ani_clock_mc._y;
              newsecondhand._x=ani_clock_mc._x;
              newsecondhand._y=ani_clock_mc._y;
              function updateClock(){
              var time= new Date();
              var hour=time.getHours();
              var minute=time.getMinutes();
              var second=time.getSeconds();
              newhourHand._rotation=(hour*360)/12
              newminuteHand._rotation=(minute*360)/60
              newsecondHand._rotation=(second*360)/60
              }

              // Frame 2
              updateClock();
              myTock.start();

              // Frame 3 ("tick" is the frame 2 label)
              gotoAndPlay("tick");
              • 4. Re: Putting sound in an Analogue Clock
                Rothrock Level 5
                Ah, I didn't think that through enough. Currently you have set it up so the second hand rotates by tiny amounts even between the seconds. You might want to have more something like this:

                function updateClock(){
                var time=new Date();
                newhourHand._rotation=time.getHours()*360/12;
                newminuteHand._rotation=time.getMinutes()*360/60;
                seconds=time.getSeconds();
                if(seconds!=lastSeconds){
                newSecondHand._roation=second*360/60
                myTock.start()
                }
                lastSeconds=seconds;
                }

                That checks to see if the seconds have changed since the last time you went through the function. Then it only moves the hand and plays the sound if the second has changed.

                Also, if you don't have anything else in frame 2 and 3, this style of coding is quite old and (IMHO) not as good. Instead, just do this on frame 1.

                this.onEnterframe=updateClock;

                or even better yet:

                clockInterval=setInterval(updateClock,100);

                That will check the clock 10 times per second.