12 Replies Latest reply on Nov 23, 2008 5:39 PM by skald89

    Flash clock code issue

    skald89
      hi when i make a flash clock on adobe flash i use this code:
      time = new Date();
      var minutes = time.getMinutes();
      var hours = time.getHours();
      If (hours < 12)
      {
      ampm="am";
      }
      if (hours >12)
      {
      ampm="pm"
      }
      if (hours <1)
      {
      hours= "12"
      ampm= "am"
      }
      if (hours>12)
      {
      hours= hours-12
      }
      if (minutes < 10)
      {
      minutes = "0" + minutes;
      }
      clock_txt.text = hours + ":" + minutes + ampm ;

      If i put i dont make a second keyframe with this code: gotoAndPlay(1);

      Whenever i preview it if the time changes on my comp my flash clock wont chnage without the code gotoAndPlay(1);
      Anyone know how to change my code so i wouldnt need the second keyframe with the code gotoAndPlay(1);?



        • 1. Re: Flash clock code issue
          kglad Adobe Community Professional & MVP
          you need to refresh your date instance (time) every time you want to update clock_txt or create one date instance and then update the time using a loop inside flash that tracks time. check getTimer().
          • 2. Flash clock code issue
            skald89 Level 1
            Im sorry can you explain that again this is my first time with Adoble flash and the code was made by someone else and i just edited it but, that was simple because it was basic mathematical logic. Are you saying that i should put in that time. check getTimer(). and it will update itself? Can you type the edited version of the code that your explaining to me please?
            • 3. Re: Flash clock code issue
              kglad Adobe Community Professional & MVP
              you should check for a flash clock tutorial so you better understand what you're trying to do.
              • 4. Re: Flash clock code issue
                skald89 Level 1
                thanks got any recommendations for my specific issue? Btw i get that the gotoandplay code is making it loop by going back to my first keyframe and continuously updating the time. I just dont know enough about adobe flash to write another code to make it update the time without that second keyframe.
                heres another code that other pple i know use and theres updates but I dont like their code because its kinda confusing for me in case i need to change the coding in it.
                _root.onEnterFrame = function ()
                {
                var _loc3 = new Date();
                var _loc1 = _loc3.getHours();
                var _loc2 = _loc3.getMinutes();
                var _loc4 = _loc3.getSeconds();
                var _loc5;
                if (_loc1 >= 12)
                {
                _loc5 = "PM";
                }
                else
                {
                _loc5 = "AM";
                } // end else if
                if (_loc1 == 0)
                {
                _loc1 = 12;
                } // end if
                if (_loc1 > 12)
                {
                _loc1 = _loc1 - 12;
                } // end if
                if (_loc1 < 10)
                {
                _loc1 = " " + _loc1;
                } // end if
                if (_loc2 < 10)
                {
                _loc2 = "0" + _loc2;
                } // end if
                if (_loc4 < 10)
                {
                _loc4 = "0" + _loc4;
                } // end if
                time_txt.text = _loc1 + ":" + _loc2 + " " + _loc5;
                time2_txt.text = _loc1 + ":" + _loc2 + ":" + _loc4 +" " + _loc5;
                hours_txt.text = _loc1 + ":";
                mins_txt.text = _loc2 + " ";
                ampm_txt.text = _loc5;
                };
                • 5. Re: Flash clock code issue
                  Ned Murphy Adobe Community Professional & MVP
                  If that code works, then you would do well to edit the variable names so that you do understand it. It is very much like your own. The key piece it has that you are missing is the first line that encloses it all... that is what makes it update continuously without the need for another frame.
                  • 6. Re: Flash clock code issue
                    skald89 Level 1
                    i did try that i tried changing all the _loc to either hours/minutes but it doesnt work anymore when i do that. Is there any code I can put in my code that I am using to make it continuously update
                    • 7. Re: Flash clock code issue
                      Ned Murphy Adobe Community Professional & MVP
                      I just told you what does that.
                      • 8. Re: Flash clock code issue
                        skald89 Level 1
                        i copied and pasted the _root.onEnterFrame = function () i opened and closed it but it either says undefined or just says the text i typed in. can someone please explain it to me like there talking to an idiot?
                        • 9. Flash clock code issue
                          Ned Murphy Adobe Community Professional & MVP
                          I've replaced _root with 'this', in the event your clock lives somewhere other than the _root timeline... I've only added these 3 lines,

                          this.onEnterFrame = function ()
                          {
                          // your code
                          }

                          and I corrected your first conditional from " If " to "if"... if this doesn't play for you, your troubles are elsewhere.

                          this.onEnterFrame = function ()
                          {
                          time = new Date();
                          var minutes = time.getMinutes();
                          var hours = time.getHours();
                          if (hours < 12)
                          {
                          ampm="am";
                          }
                          if (hours >12)
                          {
                          ampm="pm"
                          }
                          if (hours <1)
                          {
                          hours= "12"
                          ampm= "am"
                          }
                          if (hours>12)
                          {
                          hours= hours-12
                          }
                          if (minutes < 10)
                          {
                          minutes = "0" + minutes;
                          }
                          clock_txt.text = hours + ":" + minutes + ampm ;
                          }
                          • 10. Re: Flash clock code issue
                            skald89 Level 1
                            Thanks alot!! Whats the difference btwn If and if, btwn _root and this?
                            • 11. Flash clock code issue
                              Ned Murphy Adobe Community Professional & MVP
                              If (capitalized), is not code, if is... AS editor hint - if it ain't blue, it ain't Flash code

                              _root targets the main timeline, 'this' targets whatever 'this' is, which could be the _root if that's where the code is planted, or it could be inside a movieclip on the stage.

                              Look stuff up in the help docs if you wonder about it... they really are helpful once you get used to them.
                              • 12. Re: Flash clock code issue
                                skald89 Level 1
                                ok. Thanks alot for the help and im sorry if i was annoying in any way