Vectorials
Flash Perfection
3D Lessons
Tutorialkit
Markup Tutorials
Learn PHP
network
adv banner
Flash & Swish  Home Flash & Swish Flash Tutorials Using Local Shared Objects in Flash MX
rss

Using Local Shared Objects in Flash MX

Author: Phillip Kostin More by this author


Before Flash MX, it was pretty tricky to "remeber" the data in a Flash movie - it could be done with a standard browser cookie, which was hard to implement for someone with intermediate Flash skills, using a 3rd party script such as PHP or ASP or (in offline Flash applications) with the undocumented fscommand "save". Flash MX's Shared objects allow to store and retrieve the information within a Flash movie easily. Lets see how it works.

What is a local shared object ?

Flash MX Shared objects is a new feature allow you to store information at the user's machine the same way as cookies would and retreive it at a later time. Shared objects could be used to remember the user's name, the number of the level he last played in a game, his highscore or anything else you can imagine.

Shared objects are stored in .sol files located in the Flash player directory of the user's profile :

"C:/Documents and Settings/Administrator/Application Data/Macromedia/Flash Player"

and have their own format.

Here is a working example of a movie using the Shared Object. Type in your name and age and click on "Save". Then, refresh this page to let Flash read and display the stored data:

Creating a shared object

First, we have to create a local shared object within a Flash movie. To do so, just put this line of code in the first frame of your movie:

local_data = SharedObject.getLocal("user_data");

Now we have created an Object named "local_data" which is associated with a shared object on the local hard-drive named user_data. Note that in the feature, this data can be read from other movies from the same domain that created the Shared Object.

Writing data

Lets store something in our fresh created Shared Object. Lets say, we want to store the user's name and his age in it. To do so, use this:

local_data.data.user_name = "John Smith";
local_data.data.user_age = 23;
local_data.flush ()

Note that this code must be in the same level where you have created the Shared Object.

The flush () command is optional, it is used to write the information to disk immediately. If you don't use this command, Flash MX writes the shared object to a file when the SWF movie is closed or when the shared object is garbage-collected because it no longer has any references to it.

Reading data

To retrieve data from a saved Shared Object, just use the following syntax:

stored_user_name = local_data.data.user_name;
stored_user_age = local_data.data.user_age;

Don't forget that you still have to create the local_data shared object first. Now, the user's name is stored in a variable called "stored_user_name" and the user's age in the variable "stored_user_age"and you can use it anywhere in your movie.

You can store more information in one Shared Object file in the same way.

Similar to the way how we have saved simple text variables you can store whole arrays and other objects.

Important notices

There are some important things to remember when using the local Shared Objects in your movies:

1. The primary drawback of shared objects is that it can be overwritten/disabled/erased by the user (right click on the swf, and click on settings), so avoid to store information which is absolutely necessary to let the application work properly. Think of it as an additional feature.

image 1

2. The amount of the information you can store in a local Shared Object from one domain is set to 100 kb by default. If you will try to store more information, the Flash player will ask the user for a permission to increase this limit. Be sure the stage of your movie is at least this is the minimum size Macromedia Flash MX requires to display the dialog box.

image 2



Rate this Material: Bad 1 2 3 4 5 Excellent
print this page tell a friend subscribe to newsletter subscribe to rss

Add comments to "Using Local Shared Objects in Flash MX"