Publishers of technology books, eBooks, and videos for creative people

Home > Articles

  • Print
  • + Share This
From the author of

Using Functions to Repeat Common Tasks

Okay, that's enough looking around—it'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.

  1. 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.

  2. 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 6Figure 6 Formatting the offset string.

  3. Save your text file.

  4. Back in Flash MX, test your movie.

    Figure 7Figure 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.

  • + Share This
  • 🔖 Save To Your Account