Loading Data from a File

Our first experiment with data communication in Flash is quite cautious. We load a file from the server and extract a single datum from it. We continue our pursuit of practical utility by adding a simple data-loading feature to our Quiz game.

Such a useful feature would be to display an up-to-date high score. Let us presume that the highest score achieved so far is known and published on our web site. We would prefer to use XML for this purpose, but at the moment we want the simplest path from server to client, and that path is HTTP GET. It is only as the complexity and size of our data loads increase that GET becomes clearly inferior to XML, and soon utterly inadequate for the task. But to move a single scalar variable, HTTP GET makes a fine baby step.

To get URL-encoded data into Flash, use the loadVariables function. It takes one, two, or three arguments: you supply the URL, the (optional) location, and ( optionally ) the upload technique.

 loadVariables(url, scope,upLoadTechnique); 

The URL, as we have seen, is a relative or absolute document identifier. If the data file on the Internet and the .swf file is local, you need to prepend http : to your basic URL. Similarly file : is intended to perform the reverse trick.

The scope indicates the context into which these variables will load. Like all ActionScript variables , they are associated with a MovieClip. This location identifies the MovieClip either by name (target), which can be relative or absolute, or indicated by level, an ungainly and unsafe legacy of ancient Flash versions. If this argument is omitted (or the null string ("") is supplied, the context is assumed to be this.

The upload technique will be useful later. With this argument omitted, the loadVariables function call only captures data from the web; it does not return values to the server.

Notice that loadVariables() does not return a value, not even a success/error code, for two reasons. For one thing, the value being collected is arbitrarily complex. LoadVariables() can pull in many values, each paired with a name that ActionScript might not even anticipate. It is difficult to pass back a single variable whose content is so unknown. In fact, this problem is exactly addressed by XML. An XML object is a single string (or a single in-memory data structure), but it can contain information of enormous complexity and scope.

The more fundamental reason is a bit more subtle. It almost invariably trips up beginners , to whom it seems counterintuitive. With an accurate mental picture of how ActionScript executes, it is simple and obvious. The execution of loadVariables is asynchronous. When ActionScript encounters the command, it starts the operation. It then continues the code on this frame, displays the frame, and continues to the next frame. Were it to wait for a return value, the animation would freeze, which is never good. So Flash immediately executes the next ActionScript instruction while launching loadVariables as a background task. Of course the application logic might require the data before it proceeds.

It is a tenet of good interactive design to display animation of some sort during time-consuming Internet loads. Users need to be constantly assured that their computer has not crashed and that the designer's vision still guides their experience. Consequently, developers create preloaders . Ideally, a preloader is some form of progress bar that displays the status of the load operation. More often it is a simple throbber . A throbber is a bit of animation that exists simply to look alive . It distracts the user during a long load and dispels the impression that the computer has frozen up. Examples are Internet Explorer's e-globe and the Netscape comet. Either design is a trick that masks a load-and-play attitude to software. Both are distinctly inferior to a carefully staged data strategy.

Great programmers aim for a seamless experience and see each display of a preloader as an admission of defeat. Instead, they load data incrementally, hierachically, and preemptively. The application quickly loads a small amount of data that makes its basic operations functional. Meanwhile it anticipates the user's needs and preloads datasets. For example, in a quiz game, the initial download might be only enough to launch a login screen. While the player is typing in his name and perhaps reading a joke, the rest of the game is downloading.

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