- 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 in Real Time
With all the supporting functions in place, it's finally time to create the "real-time" clock. The properties of a Date object are static, so you keep them up-to-date by refreshing the object every frame. All the code within the curly brackets of the onClipEvent(enterFrame) section is run every frame, so if you create a new Date object called now within this section, it will constantly be refreshed with the current time.
Insert this code in the onClipEvent(enterFrame) section:
// Set UTC Date object now = new Date(); nowDate = dateString(now); nowTime = timeString(now);
The first line after the comments creates a new Date object called now. The next two lines pass the now object into your new formatting functions. The variables created by these functions have already been assigned to text fields at the bottom of the map.
Save your ActionScript text file.
Go back to Flash MX. Recompile and test your Flash movie.
It is important to save the text file because Flash includes this code when it compiles. If all goes well, you should now see the days, hours, and minutes tick over in real time!