< Day Day Up >
Retrieving the Data
N
ow that you've created a data-storage architecture, you need to develop a way to
retrieve the information from storage—and 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:
var dog:String = "bark";
var cat:String = "meow";
var duck:String = "quack"; 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 currentAnimal:String = "dog"; Using this syntax, we can access the value of the variable named dog:
var animalSound = this[currentAnimal]; Here, animalSound is assigned a value of "bark". The expression to the right of the equals
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.
1. 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:
2.
3. var day:String = "monday";
4.
5. var section:String = "entertainment";
6. When the application plays for the first time, we want Monday to appear initially,
and entertainment news to be displayed. The two variables set in Step 1 allow us
to accomplish this—you'll see how in the next few steps.
NOTE
These 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.
2. Select the frame labeled Sit in the Actions layer. Enter this script in that frame:
3.
4. function refresh(category) {
5.
6. section = category;
7.
8. }
9.
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.
4. With the current frame still selected, add this script to the end of the refresh()
function:
5.
6. days_mc.gotoAndStop(day);
7.
8. icon_mc.gotoAndStop(this[day].weather[0]);
9.
10. weatherBlurb_txt.text = this[day].weather[1];
11. These three actions depend on the current value of day (which we set to an initial
value of "monday" in Step 1) when they are executed. Here's how.
The movie clip instance named days_mc in the bottom-right portion of the stage
contains five buttons (M, T, W, T, and F, with instance names of monday_btn
through friday_btn) that will enable the user to select the day's news that he or she
wishes to see. This movie clip also contains five frame labels (Monday, Tuesday,
and so on), one for each day of the business week. Each of the frame labels
displays a different day in yellow. The first line of the script tells the days_mc
movie clip instance to go to the appropriate frame, based on the current value of
day. This cues the user to which day's news he or she is viewing. Because day has
an initial value of "monday", that day will initially appear in yellow.
The weather icon (instance name icon_mc) contains three frame labels, one for
each weather type (Sunny, Stormy, and Rainy). Remember that the zero element
of the weather array in the monday and tuesday objects contains one of these three
values. The second line of the script dynamically pulls that value from the weather
array using the correct object and sends the icon_mc movie clip instance to the
correct frame. Flash sees :
icon_mc.gotoAndStop(this[day].weather[0]);
9.
10. article_txt.text = this[day][section][1];
11.
12. author_txt.text = this[day][section][2];
13.
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.monday.entertainment[0];
article_txt.text = this.monday.entertainment[1];
author_txt.text = this.monday.entertainment[2]; The only text field instances affected by the current section variable are
headline_txt, article_txt, and author_txt.
7. Add this function call to the end of the frame:
8.
9. refresh(section);
10.
showing.
11. Add these button event handlers for the remaining three news section buttons:
12.
13. sports_btn.onRelease = function() {
14.
15. refresh("sports");
16.
17. };
18.
19. politics_btn.onRelease = function() {
20.
21. refresh("politics");
22.
23. };
24.
25. technology_btn.onRelease = function() {
26.
27. refresh("technology");
28.
29. };
30. This script does the same thing the ActionScript attached to the Entertainment
button does—the only difference is in the string value passed into the refresh()
function. The Politics button passes in "politics", the Sports button passes in
"sports", and the Technology button passes in "technology".
12. Choose Control > Test Movie. Click the four category buttons to see the headlines,
articles, and authors change.
The information should change very easily now. It would be easy to add more
Likewise, when the tuesday_btn is released, the day variable to changed to
"tuesday" and the news section is changed to "entertainment".
14. Choose Control > Test Movie. Click the M and T on the bottom-right of the screen
to change the display. Notice that the weather and date change.
The way we've coded this makes it easy to add day objects without changing the
information retrieval code.
15. Close the test movie and save your work as newsFlash5.fla.
You have completed a project in which displayed information is retrieved
dynamically from a logical storage structure. The next logical step in a project like
this would be to put all of our project's data into a database or text file. The
information would then be grabbed and routed into the storage structure you
created. We'll cover loading information in and out of Flash in Lesson 11
, "Getting
Data In and Out of Flash," and Lesson 12
, "Using XML with Flash."
< Day Day Up >