- It Works Like This
- Preparing to Work
- Using #include to Load ActionScript from a Text File
- Running Conditional Code with OnClipEvent
- The Time Zone Button Layer
- Using Functions to Repeat Common Tasks
- Keeping Time with the ActionScript Date Object
- Using Prototype to Extend the Date Object
- Updating in Real Time
- Updating the Mouse Time Zone
- Setting an Analogue Clock
- Setting a Row of Clocks
- Using setInterval to Update the Clocks
- About This Article
Updating the Mouse Time Zone
The next batch of code will do for the mouse zone time what the previous batch did for UTC time. The two blocks are almost identical, except for some Date modification that tweaks the Date to compensate for the time zone offset.
Back in your text editor, add this code to the onClipEvent(enterFrame) section:
// Set Zone Date object Mousezone = new Date(); Mousezone.setMinutes(now.getUTCMinutes() + (_root.zone * 60)); MousezoneDate = dateString(Mousezone); MousezoneTime = timeString(Mousezone);
You'll notice the modification in the third line. Here you take a new Date object called Mousezone and use .setUTCMinutes to add enough time to compensate for the time zone offset. The currently highlighted zone is converted to minutes by multiplying it by 60. It is then added to the current value of UTCMinutes to offset it. Once modified, the new Mousezone Date object can be interrogated as normal.
This is where the beauty of the Date object finally shows itself. If you try doing any calendar calculations using multiple individual variables, you'll soon go slightly mad. The biggest advantage to Date object mathematics is that it takes care of all the rounding stuff for you. For example, if you add 610 minutes to a Date, it knows to add 10 hours and 10 minutes, correct for new days, leap years, and all the other potential pitfalls. It's worth it!
Save your ActionScript text file.
Go back to Flash MX and recompile your Flash movie. Watch this time for the time difference as you pan around the map!
Figure 9 Real-time date and time.