16just as we can make functions more reusable by removing any literal data from them and passing it in as parameters instead, we can make entire applications reusable by designing them to receive data from a file, rather than embedding it with the code.
depending on the amount and type of data, we have several options.
the simplest way to pass data into a flash movie is as a set of attribute-value pairs that we can provide
with the flashVars parameter of the html <object> and <embed> tags.
<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000"
width="100"
height="100">
<param name="movie" value="foo.swf" />
<param name="flashvars" value="attribute1=one&attribute2=two" />
<!--[if !IE]>-->
<object type="application/x-shockwave-flash"
width="100"
height="100"
data="foo.swf"
flashvars="attribute1=one&attribute2=two" />
<!--<![endif]-->
this example passes data to the flash movie via flashvars
<!--[if !IE]>-->
</object>
<!--<![endif]-->
</object>
note that this is equivalent to providing the values as url parameters directly on the swf:
http://accad.osu.edu/~pgerstma/class/pca/examples/flashvars/flashvars.swf?msg=hello&favoriteColor=green
the attribute-value pairs take the form attributeName=attributeValue,
separated (delimited) by ampersands, with no extra spaces.
values may have spaces in them, but attribute names cannot.
all url parameters to a swf are available in code through a property called parameters on the root timeline's instance of
LoaderInfo
the attribute names of the url parameters become property names, and the attribute values become string values of the properties.
some characters would be confusing to the browser if entered directly as part of an attribute value, so browsers use percent codes, or URL encoding to handle problematic characters like:
␠ $ + , " < > % { } | \ ^ ~ [ ] `
as:
␠ $ + , " < > % { } | \ ^ ~ [ ] `
%20 %24 %2B %2C %22 %3C %3E %25 %7B %7D %7C %5C %5E %7E %5B %5D %60
more info:
flash supports a very limited form of writing data to the local hard drive. similar to a javascript cookie, a SharedObject can be used to store a small amount of data that will stays on the user's computer even after the swf has been closed. multiple swfs can read and write to the same shared object on the same machine.
using a shared object involves first calling the getLocal() method of the SharedObject class,
giving it a name for the shared object to retrieve or create. getLocal will return a shared object instance
that has a data property. data is an Object instance, and any properties that you add to it
will be stored in the shared object on disk. you can force a save of data by calling flush(), but the
flash player will automatically do this for you when the swf is closed.
var LSO:SharedObject = SharedObject.getLocal("myCookie"); LSO.data.myProperty = "anything i want";
for loading simple text data, flash provides the URLLoader class.
using it involves creating a new instance, adding listeners for at least the Event.COMPLETE event,
and then calling the load method on the URLLoader instance,
passing it the path to a data file wrapped in a URLRequest instance.
function onDataReady(e:Event):void { trace("File contents: " +loader.data); } function onError(e:Event):void { trace("Error loading file: " +e); } var loader:URLLoader = new URLLoader(); loader.addEventListener(Event.COMPLETE, onDataReady); loader.addEventListener(IOErrorEvent.IO_ERROR, onError); loader.load(new URLRequest("simpleMap.txt"));
when the flash player has fully loaded the data file, it will execute the handler for the
Event.COMPLETE event, where the data can be retrieved from the loader instance's data property.
as a convenience in the example below, a URLVariables instance is used to parse the data into properties of an Object.
this is possible because the data in the file is url-encoded.