Instead of testing to see whether a variable has magically sprung into existence ”

 if ( highscore ) {      score=highscore;      stop();      } 

”we can now test the XML object we have created:

 if ( highscore.loaded ) { 

Every XML object has a public boolean property called .loaded . Testing this is prettier and a lot more robust than looking for data to appear. Something other than a successful load could trip the original test by causing highscore to appear on this frame. Conversely (and more commonly) a good load of a bad file would give false negatives . By contrast, the XML conditional directly tests the success of the load operation.

Even this improved test is suboptimal. We are still rolling around in loops polling a flag. Modern programmers would prefer an efficient event-driven design. So shall we, but later. We return to this issue after we make the code do something.

First, we make the code reassure us that it is working by tracing out this message:

 if (this.highscore.loaded) {       trace( highscore.toString());       score=highscore;       stop();       } 
 <?xml version="1.0" encoding="UTF-8"?><score>122</score> 

Reassuringly, this is identical to the XML file. However, it is not the original XML string. It is a fresh XML encoding of the object created by parsing the original string. This round-trip can be demonstrated by adding syntactically insignificant characters to the data file, such as trailing space inside a tag ”

 <?xml version="1.0" encoding="UTF-8"?><score    >122</score> 

”or comments (which Flash legitimately discards):

 <?xml version="1.0" encoding="UTF-8"?><!--comment--><score>122</score> 

Neither of these changes to the input string alters the output string at all.




Note that we did not remove the line that assigns highscore (the loaded data) to score (the variable linked to the onscreen display):


When we look at the display, however, we see this:

Why is this?

When this code executes, the variable score is assigned the same string we saw in the trace statement.

 <?xml version="1.0" encoding="UTF-8"?><score>122</score> 

The Flash interpreter invokes the toString() method when assigning a complex object (like this XML object) to a scalar variable ”which it considers a string variable. But why does the display show 108122?

When we set up the text field that displays the score, we used a special font. To minimize the download, we saved only outlines for the digits in the font. This is fine when we are displaying actual scores. Letters and symbols, however, are simply not displayed. It is interesting to note that even the minus sign ( “) and period (.) ”so useful in displaying numbers and so simple in every font ”were not automatically included.

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

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