Isolating the Data


We are well along toward our goal of separating content and presentation. But all the data is still coded directly into the presentation component.

Let's collect the presentation functionality into its own MovieClip symbol. Flash does not make this easy, but with much cutting and pasting of keyframes we can make a symbol called Round . The base movie now has a single instance (called round ) of this symbol.

As a first rough step, we can quickly strip all the data out of round and stuff it into a special Round object in the base movie.

ActionScript
 r = new Object();    r.q  = "<p align='center'>Which of these names...    r.a0 = new Object();          r.a0.text="William the Conqueror";    r.a1= new Object(); etc... 

You get the idea. We are defining the data structure as we need it. It is an irregular tree that models exactly the data we have on hand. It packages all this data into a single variable r . Once r is filled we can copy into round .

ActionScript
 ActionScript round.r=r; stop (); 

Now the round must be smart enough to get its data from the single object. In this rough first step, we simply copy out of the object the nine pieces of data that we know it contains. We do this one by one (Figure 3.6). Within round the data is distributed into the proper components .

Figure 3.6. Copying Answers Out of a Round

graphics/03fig06.jpg

So far we have broken nothing; it still works. And the data is removed from the round proper. But it is still a clumsy system. We can clean it up by creating a regular data structure. Returning to the initial frame of the base movie, we establish an object Question , which is composed of q (the text of the question) and an array of Answers . The Answer object is the text , the score earned, and a comment .

These structures are defined by their constructors:

ActionScript
 function Question( questiontext, answerarray ){     this.q= questiontext;     this.a= answerarray; } function Answer( text, score, comment ){     this.text=text;     this.score=score;     this.comment=comment; 

These constructors are invoked (as in similar languages) when the object is first created.

A shortfall compared to similar languages is the lack of multiple signatures . The Answer constructor requires us to supply the text, score , and comment . Classically, we might create another Answer constructor that is distinguished by requiring only text . We would write this constructor to assume the obvious and create a wrong answer option with no comment. This would save coders and readers the tedium of lines filled with formulaic emptiness. But we do not have such a feature, as can be seen by this initialization (in which long lines of text have been truncated for legibility):

ActionScript
 new Question ( "Which of these names does<br>                 not belong with the others?",         new Array(             new Answer ("William the Conqueror",  0, ""),             new Answer ("William the Bald", 1, "<b>William the ..."),             new Answer ("William the Bastard", 0, ""),             new Answer ("William the Duke of Normandy", 0, "Hint...")             )         ); round.r=r; stop (); 

However, we are not forced to do things in such an ugly way. We can work around Flash's lack of multiple signatures. Any parameters we do not pass to our constructor will be of the undefined type, although it is important to note that the missing parameters will be assumed to be the last ones. For instance, if we typed in something like

ActionScript
 new Answer ("William the Bastard", "Who would have the guts to call him              that?"); 

the constructor would assume that the text was "William the Bastard" and the score was "Who would have the guts to call him that?" It would assume that comment was of the type undefined.

Now that we know that the parameters may be undefined, we can modify our constructor to take different signatures.

ActionScript
 function Answer( text, score, comment ) {    this.text = text;    if (typeof(score)=="undefined")       {       this.score = 0;       this.comment = "";       }    else       {       this.score = score;       if( typeof(comment)=="undefined")          this.comment = "";       else          this.comment = comment;       }    } 

This code is longer but more robust. There are several ways we could improve it, they are left as exercises for readers to do as much or little of as they see fit. We could check to see whether score was a string or a number (such as 1) when there is no comment . Then, if it were a string, we could presume that comment was intended to be present and the score omitted, and write our code accordingly . We could use the conditional operator to tighten its appearance.

Note

C ONDITIONAL O PERATOR

The conditional operator is an operator much like plus or times. Unlike those two, the conditional operator takes in three values. The first value is a boolean and usually is the result of some equality check or comparison such as variable>1 . A question mark is placed after the first value. Then a second value is added. This value will be returned by the conditional operator if the first variable evaluates to true. A colon separates the second from the third value, which is returned if the first value equates to be false. This has the same effect as an if/else statement whose sole purpose is to assign one value or another to a variable.

For example,

ActionScript
 IsXOne = (X==1) ? "Yes" : "No";  IsXTwo = (X==2) ? "Yes" : "No";  this.score   = (typeof(score)   == "undefined")?  0 : score;  this.comment = (typeof(comment) == "undefined")? "" : comment; 

Regardless of how we end up writing the code, again we have packaged all our information into a single variable r , but now we have been far more rigorous . Though Flash will actually do little to enforce it, we have a well-defined data structure that is both predictable and flexible.

Note that this definition does not demand that our questions have four answer options. As we go further we will work to avoid and eradicate such assumptions. For example, we need to rewrite the step in round where the data object is deconstructed and the presentation screen is created. Round needs to be made aware of the new question structure. An intelligent for loop avoids explicit assumptions about how many answers exist. But the graphics still assume four answers: Round contains four MovieClip instances ( answer0, answer1 answer3 ). These AnswerOption objects are addressed by constructing each name and using eval to treat the string as an object name .

ActionScript
 q = r.q; for( var i=0; i< r.a.length; i++){   eval("answer"+i).text   = r.a[i].text;   eval("answer"+i).score  = r.a[i].score;   eval("answer"+i).comment= r.a[i].comment;   } stop (); 


Flash and XML[c] A Developer[ap]s Guide
Flash and XML[c] A Developer[ap]s Guide
ISBN: 201729202
EAN: N/A
Year: 2005
Pages: 160

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net