- 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
- Summary
- About This Article
Using Functions to Repeat Common Tasks
Okay, that's enough looking aroundit's time to get coding! Because the value of zone is updated every time the user rolls over a new zone, the map is already interactive. With a little cosmetic alteration, you can take the value of zone and display it onscreen as the user pans around the map.
NOTE
The international dateline is the black zigzag line on the map. This line is exactly 180 degrees east of the prime meridian (the straight line) that runs through Greenwich in London. The line itself is a little ragged because each territory along the border has arbitrarily chosen which side of the dateline it wants to be on. Travelers crossing this line going eastward must set their calendars back by one day. Westward travelers must set their calendars forward by one day.
This tutorial is going to make good use of functions. A function is a block of code that is designed to be frequently reused. Some functions take parameters, some return values, and others do neither. Also, every function needs to be defined before it can be used, which is why you place them within the onClipEvent (load) section.
Go back to your text editor and add this code below the FUNCTION LIBRARY title of the onClipEvent (Load) section:
// FUNCTION: to create timezone offset string function setTimezoneOffsetString(zone) { if (zone > 0) {zone = "+" + zone}; return zone; }
This first function takes the integer zone and creates from it a string called timezoneOffsetString. The code inside a function can be as long or as short as you like; in this case, it is a single if condition. If the value of zone is negative, the returned string is identical. If the value of zone is positive, the string is returned with a preceding plus sign.
Place this function call within the onClipEvent (enterFrame) section:
// Set Time Zone Offset String TimezoneOffsetString = setTimezoneOffsetString(_root.zone);
This code ensures that the timezoneOffsetString is constantly updated by placing the function call within the onClipEvent (enterFrame) section and feeding it the current value of zone.
Figure 6 Formatting the offset string.
Save your text file.
Back in Flash MX, test your movie.
Figure 7 The published movie in flash mx
If you pan around the map with your mouse, you should see the time zone offset displayed in the middle circle at the bottom of the screen. Congratulations, you've made a start!
So far, you have used purely fixed values to demonstrate the difference in time between UTC and each zone. This approach limits you to only showing comparisons. To show the actual time in each zone, you must now introduce the Date object.