View Single Post
Nutrox's Avatar Nutrox Nutrox is offline Super Moderator 17 Creative Assets 2006-11-05 #19 Old  
016 - Realtime Clock (tick tock)
 
Copy and paste the following code into a new Flash document for an instant clock. :)

ActionScript Code:
  1. var hourHand:MovieClip;
  2. var minuteHand:MovieClip;
  3. var secondHand:MovieClip;
  4. var clockInterval:Number;
  5.  
  6. createClips();
  7. updateClock();
  8. clockInterval = setInterval(this, "updateClock", 500);
  9.  
  10. //
  11.  
  12. function updateClock():Void {
  13.     var d:Date   = new Date();
  14.     var h:Number = d.getHours();
  15.     var m:Number = d.getMinutes();
  16.     var s:Number = d.getSeconds();
  17.    
  18.     if (h >= 12) {
  19.         // getHours returns a value between 0 and 23,
  20.         // so we need to keep it within a 12 hour range.
  21.         h -= 12;
  22.     }
  23.    
  24.     hourHand._rotation   = (360 / 12) * h;
  25.     minuteHand._rotation = (360 / 60) * m;
  26.     secondHand._rotation = (360 / 60) * s;
  27. }
  28.  
  29. function createClips():Void {
  30.     var depth:Number = this.getNextHighestDepth();
  31.     // Second
  32.     secondHand = this.createEmptyMovieClip("secondHand", depth++);
  33.     secondHand.lineStyle(2, 0xFF0000);
  34.     secondHand.moveTo(0, -80);
  35.     secondHand.lineTo(0, 0);
  36.     // Minute
  37.     minuteHand = this.createEmptyMovieClip("minuteHand", depth++);
  38.     minuteHand.lineStyle(3, 0x00FF00);
  39.     minuteHand.moveTo(0, -90);
  40.     minuteHand.lineTo(0, 0);
  41.     // Hour
  42.     hourHand = this.createEmptyMovieClip("hourHand", depth);
  43.     hourHand.lineStyle(5, 0x0000FF);
  44.     hourHand.moveTo(0, -100);
  45.     hourHand.lineTo(0, 0);
  46.    
  47.     // Position the movie clips.
  48.     // Just move them to the center of the Stage for now.
  49.     var x:Number  = Math.floor(Stage.width / 2);
  50.     var y:Number  = Math.floor(Stage.height / 2);
  51.     hourHand._x   = x;
  52.     hourHand._y   = y;
  53.     minuteHand._x = x;
  54.     minuteHand._y = y;
  55.     secondHand._x = x;
  56.     secondHand._y = y;
  57. }