< Day Day Up >
Using Shared Objects
An SWF file can save data (variables as well as array, XML, and other data objects) to a
user's hard drive using shared objects—similar to but more powerful than the cookies
used by Web browsers. You can use shared objects to store information generated by the
user while viewing your movie (name, last frame visited, music preference, and so on).
Shared objects can be used by movies played in a Web browser as well as those turned
into stand-alone projectors.
You can use shared objects with any of the following (for example):
•
XML.load
•
XML.sendAndLoad
•
LoadVars.load
•
LoadVars.sendAndLoad
•
LoadVariables
•
LoadVariablesNum
•
XMLSocket.connect
•
Importing a shared library
The following is an example of a script you might use to create a shared object:
var myObject:SharedObject = SharedObject.getLocal("stuff_I_saved");
If the shared object stuff_I_saved already exists on the user's hard drive, its data is loaded
instantly into myObject. If stuff_I_saved does not yet exist, it's created and still
referenced by myObject. In the latter case, myObject would be empty—that is, it would
contain no data.
NOTE
If used as just mentioned, the getLocal() method will create a shared object if none exists,
or will retrieve data from an existing shared object.
As you can see from the previous syntax, the shared object's name is actually
"stuff_I_saved"; however, in ActionScript you can't reference the shared object directly
using that name. Therefore, a reference to the shared object is created using myObject.
This means that whenever you reference myObject in a script, you're actually referencing
the shared object named "stuff_I_saved"—a tricky concept but essential to understanding
how ActionScript deals with shared objects.
Data is saved to a shared object using the data property. Take a look at the following
example:
myObject.data.userName = userName_txt.text;
This would save the userName variable (and its value, the text in the userName_txt text
field) in the shared object. You can save entire objects as well. For example, if you
wanted to save an array contained by your project, you would use the following syntax:
myObject.data.savedArray = nameOfArray;
A single shared object can contain multiple bits of data simultaneously:
myObject.data.savedArray = nameOfArrayObject;
myObject.data.savedXML = nameOfXMLObject;
myObject.data.userName = userName_txt.text;
A particular piece of data can be erased from a shared object using null, as in the
following example:
myObject.data.userName = null;
If userName were a piece of data in the shared object, the preceding script would delete
it.
You can delete an entire shared object by using the clear() method of the SharedObject
class:
myObject.clear();
Extracting data from a shared object is similar to creating data in one:
userName_txt.text = myObject.data.userName;
In the userName_txt text field, the preceding script will display the value of userName in
the shared object. If this variable doesn't exist in the shared object, the value displayed in
the text field will be undefined.
When the SWF session ends (that is, the movie is closed or exited), all the information
under the data property of your shared object is automatically written to the shared object
file, ready to be retrieved using the getLocal() method described earlier. You can force a
shared object to be written and saved at any time by using the flush() method. For
example:
myObject.flush();
This line of ActionScript forces your shared object and all the data it contains to be
saved. Because myObject references the shared object named "stuff_I_saved", this is the
object that will actually be saved.
Flash stores all shared objects in a central location on the user's hard drive—the exact
location depends on where the movie resides that created the shared objects.
On Windows XP, all shared objects are stored in the following general directory:
Documents and Settings\<username>\Application Data\Macromedia\Flash Player\
where <username> is the name of the user who was logged on when the shared object
was created.
On a Mac, the location is as follows:
System Folder\Preferences\Macromedia\Flash Player\
TIP
Depending on the version of your operating system, the location of shared object files
may vary somewhat. To locate shared object files on your machine, search for files with
an .sol extension.
These are both general paths—that is, when a movie creates a shared object, a new
subdirectory is created at one of the previously mentioned locations. For example, if you
were to view a movie at the following URL:
any shared object created by this movie would, by default, be saved at the following path
on a Windows machine:
Documents and Settings\<username>\Application Data\Macromedia\Flash
Player\electrotank.com\fun\games\MiniGolf
Notice how this subdirectory's path structure matches that of the URL.
Because a movie played locally (such as a projector) doesn't exist at a URL, Flash will
save shared objects that it creates to a localhost directory:
Documents and Settings\<username>\Application Data\Macromedia\Flash
Player\localhost
All these directory paths are default paths where shared object data is stored. You
actually have a lot of latitude as to where a shared object is stored or retrieved from
within the general directory. Using the previous example, imagine playing a movie at the
following URL:
This movie has the following shared object:
myScores = SharedObject.getLocal("scoreData");
This shared object is saved to the following path in Windows XP:
Documents and Settings\<username>\Application Data\Macromedia\Flash
Player\electrotank.com\fun\games\MiniGolf\scoreData.sol
Flash will look for this same location again when the movie is played from that URL;
however, the getLocal() method lets you add an optional directory path where the shared
object should be saved. Assuming the movie at the aforementioned URL has this shared
object declaration:
var myScores:SharedObject = SharedObject.getLocal("scoreData", "/fun");
the shared object would be saved to the following path:
Documents and Settings\<username>\Application Data\Macromedia\Flash
Player\electrotank.com\fun\scoreData.sol
Armed with this knowledge, you can create movies at different locations that use the
same shared object—useful if you want all the movies on your site to reference a
"master" shared object containing information about the user. Simply save a shared
object in the main (/) directory.
Be careful when using a single shared object across movies. Any one of the shared
objects has the potential of overwriting the data it contains with new data.
A single movie can create, save, and load multiple shared objects simultaneously.
TIP
You can configure the amount of data that a given URL can store by using the Flash
player. If you right-click the window of an open SWF and select Settings, you'll see the
Local Storage controls. You can block any site from storing information on your
machine.
In this exercise, you'll create a journal that saves text entries in an array as a shared
object.
1. Open journal1.fla in the Lesson11/Assets folder.
You will notice one frame with four layers, named according to their contents. The
stage contains two text fields that will be used to display information. The large
text field in the center, journalBody_txt, will be used for journal entries. The
smaller text field at the bottom of the screen, entryNumber_txt, will be used to
display the current journal entry number. The Buttons layer contains the Prev,
Next, New, and Save buttons, which have instance names of previous_btn,
next_btn, new_btn, and save_btn, respectively.
This application will allow you to start a new journal entry, save it, and browse
through the entries you've created.
2. With the Actions panel open, select Frame 1 in the Actions layer and then add the
following script:
3.
4. var myJournal:SharedObject = SharedObject.getLocal("JournalObject");
5.
This line of ActionScript creates a reference to the shared object JournalObject.
This object can be read and modified using the myJournal reference set up here.
When using myJournal in the following scripts, we're actually working with the
shared object named JournalObject.
3. Add the following conditional statement just below the line of script you added in
Step 2:
4.
5. if (myJournal.data.journal == undefined) {
6.