User Guide
392 Networking and Communication
}
trace(so.data.now);
trace("SharedObject is " + so.size + " bytes");
so.flush();
When using the flush() method to write shared objects to a user’s hard drive, you should be
careful to check whether the user has explicitly disabled local storage using the Flash Player
Settings Manager (http://www.macromedia.com/support/documentation/en/flashplayer/
help/settings_manager07.html), as shown in the following example:
var so:SharedObject = SharedObject.getLocal("test");
trace("Current SharedObject size is " + so.size + " bytes.");
so.flush();
Values can be retrieved from a shared object by specifying the property’s name in the shared
object’s
data property. For example, if you run the following code, Flash Player will display
how many minutes ago the SharedObject instance was created:
var so:SharedObject = SharedObject.getLocal("test");
if (so.size == 0)
{
// Shared object doesn't exist.
trace("created...");
so.data.now = new Date().time;
}
var ageMS:Number = new Date().time - so.data.now;
trace("SharedObject was created " + Number(ageMS / 1000 /
60).toPrecision(2) + " minutes ago");
trace("SharedObject is " + so.size + " bytes");
so.flush();
The first time the previous code is run, a new SharedObject instance named test will be
created and have an initial size of 0 bytes. Because the initial size is 0 bytes, the
if statement
evaluates to
true and a new property named now is added to the local shared object. The
shared object’s age is calculated by subtracting the value of the
now property from the current
time. Each subsequent time the previous code is run, the size of the shared object should be
greater than 0, and the code will trace how many minutes ago the shared object was created.
Displaying contents of a shared object
Values are stored in shared objects within the data property. You can loop over each value
within a shared object instance by using a
for..in loop, as the following example shows:
var so:SharedObject = SharedObject.getLocal("test");
so.data.hello = "world";
so.data.foo = "bar";
so.data.timezone = new Date().timezoneOffset;
for (var i:String in so.data)