Retrieving the Data
Now that you've created a data-storage architecture, you need to develop a way to retrieve the information from storageand that involves some new syntax that provides a means of writing a dot syntax path dynamically. Square brackets are used to evaluate an expression. Here's how it works. Assume we have three variables that contain animal sounds:
We'll create a variable and assign it a text value of "dog", "cat", or "duck" (note that these text values have the same names as the variables we just mentioned). We'll start with "dog":
var dog:String = "bark";
var cat:String = "meow";
var duck:String = "quack";
Using this syntax, we can access the value of the variable named dog:
var currentAnimal:String = "dog";
Here, animalSound is assigned a value of "bark". The expression to the right of the equals sign looks at currentAnimal and sees that it currently contains a string value of "dog". The brackets surrounding currentAnimal tell ActionScript to treat this value as a variable name. Flash sees this line of script like this:
var animalSound = this[currentAnimal];
Because dog has a value of "bark", that's the value assigned to animalSound. If we were to change the value of currentAnimal to "cat," animalSound would be assigned a value of "meow".Note that you must use this in the expression to include the target path of the variable used to set the dynamic variable name (in this case currentAnimal), because it relates to the variable to the left of the equals sign (in our case animalSound). Using this denotes that these two variables are on the same timeline. If animalSound existed in a movie clip instance's timeline while currentAnimal was on the root (main) timeline, the syntax would look like this:
var animalSound:String = this.dog;
Here are a couple of other examples:
var animalSound:String = _root[currentAnimal];
This syntax is critical to the way you retrieve information from the objects in this exercise. Remember that all the objects are built with the same array names and structure; they have different parent object names (monday and tuesday). We can use the aforementioned syntax to dynamically access the data in these objects based on the current value of a variable.
var animalSound:String = _parent[currentAnimal];
var animalSound:String = myMovieClip[currentAnimal];
With newsFlash4.fla open, select Frame 1 of the Actions layer. Open the Actions panel and enter these variables at the end of the current script:
var day:String = "monday";
var section:String = "entertainment";

NOTEThese variables are known as initializing variables. Because the information our application displays depends on the buttons the user clicks, these variables provide some starting settings prior to user input.Select the frame labeled Sit in the Actions layer. Enter this script in that frame:
This function is called when any of the section buttons is clicked. The button events that call this function will be added later in this exercise.The purpose of this function (by the end of this exercise) is to refresh what is shown on the screen based on the current values of the day and section variables (which were initially set to "monday" and "entertainment", respectively, in Step 1 of this exercise).Currently there is only one line of ActionScript inside this function definition. It updates the value of the section variable to that of the parameter value passed to the function. If the Entertainment button is clicked, it will call this function and pass it a value of "entertainment". The function will then assign the section variable a value of "entertainment". We'll explain the purpose over the next several steps as we add more actions to this refresh() function.Add this script at the end of the refresh() function:
function refresh(category) {
section = category;
}
This script uses the syntax we introduced at the beginning of this exercise. The text displayed in the date_txt field is set by dynamically referencing another variable. Because day initially has a value of "monday" (as set in Step 1), ActionScript sees the code as this:
date_txt.text = this[day].date;
You'll remember that monday.date contains the text value of "Monday, August 25 2003" and date_txt is the name of the text field in the bottom-left portion of the screen. As a result of this action, "Monday, August 25 2003" will be displayed in the date_txt text field.You can begin to see how dynamically named variables can be useful. If the variable day had a value of "tuesday", this line of code would reference the date variable in the object called tuesday.With the current frame still selected, add this script to the end of the refresh() function:
date_txt.text = monday.date;
days_mc.gotoAndStop(day);
icon_mc.gotoAndStop(this[day].weather[0]);
weatherBlurb_txt.text = this[day].weather[1];

as
icon_mc.gotoAndStop(this[day].weather[0]);
Let's take it a step further. Consider that monday.weather[0] has a value of "Rainy":
icon_mc.gotoAndStop(this.monday.weather[0]);
In the same way, the last action will populate the weatherBlurb_txt text field with the value of monday.weather[1], which is "Very wet".Keep in mind that if day had a value of "tuesday", these actions would be executed based on the respective values in the tuesday object.Add this script to end of the refresh() function:
icon_mc.gotoAndStop("rainy");
Using the same syntax as in the preceding step, the high_txt and low_txt text fields are populated by referencing the second and third elements of the weather array dynamically.Add these actions to the refresh() function:
high_txt.text = this[day].weather[2];
low_txt.text = this[day].weather[3];
The dynamic referencing performed in this step is one level deeper into the storage objects than the dynamic referencing was in Step 5. We're trying to dynamically pull information about a news article from an object and an array because the day can be either Monday or Tuesday and the section can be Entertainment, Politics, Sports, or Technology. The initialization variables you set in Step 1 of this exercise set section = "entertainment". Using the values of our initialization variables, Flash will read the three lines of ActionScript like this when the refresh() function is executed:
headline_txt.text = this[day][section][0];
article_txt.text = this[day][section][1];
author_txt.text = this[day][section][2];
The only text field instances affected by the current section variable are headline_txt, article_txt, and author_txt.Add this function call to the end of the frame:
headline_txt.text = this.monday.entertainment[0];
article_txt.text = this.monday.entertainment[1];
author_txt.text = this.monday.entertainment[2];
refresh(section);

This scripts the Entertainment button so that when it is released, the refresh() function is called and the string "entertainment" is passed as the section name. This changes the display to an entertainment-related article if one isn't already showing.Add these button event handlers for the remaining three news section buttons:
entertainment_btn.onRelease = function() {
refresh("entertainment");
};
sports_btn.onRelease = function() {
refresh("sports");
};
politics_btn.onRelease = function() {
refresh("politics");
};
technology_btn.onRelease = function() {
refresh("technology");
};

days_mc.monday_btn.onRelease = function() {
day = "monday";
refresh("entertainment");
};
days_mc.tuesday_btn.onRelease = function() {
day = "tuesday";
refresh("entertainment");
};
