Accessibility

TechNote (Archived)

Using external data to customize the Flash 5 Menu smart clip

The Menu smart clip that ships with Macromedia Flash 5 (Windows> Common Libraries > Smart Clips) allows you to easily add a pop-up menu to a Flash movie without a lot of scripting.

See Customizing the Flash 5 Menu Smart Clip (TechNote 15140) for the basics of using the menu smart clip. This TechNote explains how to populate the menu's pop-up items using an external data source. This simple technique can be broken down into three basic parts:

  1. Create an external data source.
  2. Load the external values into Flash usingloadVariables.
  3. Use the onClipEvent(data) event handler to determine when the data has finished loading, and then insert the values into the menu.

Keep in mind that this simple method has no error-checking. If the data file doesn't load;for whatever reason;then no items will appear in the pop-up menu.

Creating the data source

This example uses a text file data source. However, middleware applications such as ASP, CGI, or ColdFusion could be used to retrieve the data. Refer to the section that discusses usingloadVariables in Different approaches to setting variables in a Flash movie (TechNote 14253) and An overview of integrating Flash with a database (TechNote 14482) for more detail on loading external data into Flash.

Steps to create the data source:
  1. Open a new text file and enter the following text without any line returns:
     myItemsVar=one,two,three,four,five,six 
    The comma-separated items on the right are those that are going to populate the menu.

    Note: More than one variable can be set from a text file. Multiple variables are separated by the '&' character. This allows data to be fed to multiple menu smart clips using a single data source. For example, the following contains two variables,myItemsVar and secondItemsVar:
     myItemsVar=one,two,three,four,five,six&secondItemsVar=alpha,beta 
  2. Save the text file as datafile.txt to the same folder that contains the Flash movie with the menu smart clip (if you haven't yet created the menu yet, see the next step).

    Note that when the movie is run off a Web server, or any other location, the text file needs to be in the same directory as the published SWF. The format of the file must be text-only. Rich text or other word processing formats will fail to load the variables.
Load the external data

The loadVariables action is used to retrieve the data from the text file and send it to the menu smart clip.

Steps to load the external data:
  1. In Flash, choose Window > Common Libraries> Smart Clips. Place an instance of the Menu smart clip on the stage.
  2. Select the menu instance and open the Instance Panel (Window> Panels > Instance). Give the menu clip an instance name ofmenuClipMC.
  3. Double-click the first frame on the main timeline to open the Actions panel and enter the following action:

    loadVariables ("datafile.txt","menuClipMC");

    This action loads the variable data from the external file and sends it directly to the menuClipMC clip.
Catch the data and populate the menu's items

An onClipEvent(data) handler, attached tomenuClipMC, is used to determine when the data from the text file has fully loaded. Within that handler, the String.split method separates the the comma-separated items into an array, which is then used to populate the menu items in the smart clip.

Steps to populate the menu items:

Select the menuClipMC instance and open the Actions window (Window > Actions). Attach the following ActionScript to the instance:

 onClipEvent(data){ this.items = myitemsVar.split(','); this.currentValue = items[0]; } 

In this case, the split function separates the six items in the data string; "one,two,three,four,five,six";into six array items"one","two","three","four","five","six". This new array is then assigned to the items array, which the smart clip uses internally to populate the menu.

The last action, this.currentValue = items[0], sets the first item displayed in the menu to the first item in the array. Omitting this line will cause the menu to display the default text 'defaultValue1'.

Additional Information


For more information on using data in a Flash movie, please refer to External data not accessible outside a Flash movie's domain (TechNote 14213) and Attempts to send data from a Flash movie are not working (TechNote 14234).

AlertThis content requires Flash

To view this content, JavaScript must be enabled, and you need the latest version of the Adobe Flash Player.

Download the free Flash Player now!

Get Adobe Flash Player

Creative Commons License

Search Support


Document Details

ID:tn_15197
Browser:Chrome
Internet Explorer
Netscape
Opera
Safari
Firefox
Database:DB2
Informix
MySQL
Oracle
SQL Server
Sybase
MS Access

Products Affected:

flash