SharedObject Object | Flash 6 |
local data storage and remote data transmission |
None; instances are created with getLocal( ) or getRemote( ).
Container for the SharedObject's data.
Closes the connection between a remote shared object and Comm Server. See the Comm Server documentation.
Connects to a remote shared object on Comm Server. See the Comm Server documentation.
Forces data to be written to disk.
Returns a reference to a specified local SharedObject instance.
Returns a reference to a remote SharedObject instance managed by Comm Server. See the Comm Server documentation.
Returns the size of the SharedObject, in bytes.
Sets the frequency with which changes to a remote shared object are sent to Comm Server. See the Comm Server documentation.
Invoked when a SharedObject generates a status message (e.g., an error, information, or a warning).
The SharedObject object provides tools for storing ActionScript data on the end user's computer and transferring data between Flash clients via Comm Server. When used for local data storage, the SharedObject is referred to as a local SharedObject (LSO). When used for remote data transfer, the SharedObject is referred to as a remote SharedObject (RSO). This section covers only the local data storage capabilities of SharedObject. Information on remote data transfer features can be found in the Comm Server documentation at:
Like JavaScript cookies in a web browser, a local SharedObject saves information permanently on the local filesystem. When a movie is closed, the information is stored in a file. When the movie is reopened, the information is available once again. This allows movies to, say, record a player's high score and make it available across multiple game sessions. Local SharedObjects are more flexible than JavaScript cookies. They allow typed data to be stored and accessed directly through object properties rather than through an awkward cookie string of name/value pairs. Specifically, SharedObjects support the following datatypes: number, string, boolean, undefined, null, array, and object (custom classes plus XML and Date). Other built-in ActionScript classes and objects (such as MovieClip and Function) cannot be stored in a SharedObject.
We do not create SharedObject instances with a constructor function. Instead, to create a new local SharedObject, we use the getLocal( ) method, which takes the name of the SharedObject to create as an parameter:
player1Info_so = SharedObject.getLocal("player1");
If a SharedObject by the specified name ("player1") already exists for the current movie, this code retrieves it from the local filesystem; if no such object exists, the Player creates it. Note the use of the suffix "_so"; this is Macromedia's official SharedObject suffix, which will enable code hinting in future versions of Flash.
After our shared object is created (or retrieved), we can add information to it by assigning custom properties to the built-in data property, such as:
player1Info_so.data.highScore = 1200;
|
Once saved, the SharedObject can be retrieved later with the getLocal( ) method, exactly as it was created:
// Load the SharedObject we created earlier player1Info_so = SharedObject.getLocal("player1");
Any properties defined on the object can be used immediately after getLocal( ) is invoked to retrieve the LSO. For example, here we set the value of the hiScore_txt text field to the value of player1Info_so.data.highScore, which we created earlier:
hiScore_txt.text = player1Info_so.data.highScore;
By default, movies from any given domain can store a cumulative total of up to 100 KB of data on the user's computer. For information on requesting more space and handling storage requests that exceed the user's defined space limits, see SharedObject.flush( ).
For a prolonged example showing how to store address book contacts with a SharedObject, see the following article, written by me:
The following code creates a SharedObject, named userProfile, that tracks the total amount of time the user has spent viewing the movie:
var userProfile_so = SharedObject.getLocal("userProfile"); trace(userProfile_so.data.totalVisitTime); var then = 0; this.onEnterFrame = function () { var now = getTimer(); var frameTime = now - then; userProfile_so.data.totalVisitTime += frameTime; this.then = now; }
"Object Properties," in Chapter 12