Instead of testing to see whether a variable has magically sprung into existence ” ActionScriptif ( highscore ) { score=highscore; stop(); } ”we can now test the XML object we have created: ActionScriptif ( 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: ActionScriptif (this.highscore.loaded) { trace( highscore.toString()); score=highscore; stop(); } Output<?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<?xml version="1.0" encoding="UTF-8"?><score >122</score> ”or comments (which Flash legitimately discards): XML<?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
|