Flash Shared Object: The Flash Cookie Tutorial

Flash Shared Object: The Flash Cookie Tutorial

Flash Shared Object is an Adobe Flash technology that allows a movie to be stored on the user’s computer, where it can be referenced in other websites or programs. You will explore this technology and what it means for website development in this tutorial.

What is a local shared object?

Flash MX Shared object is a new feature that allows you to store information at the user’s machine the same way as cookies would and receive 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 high score, 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:

tlocal_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

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

tlocal_data.data.user_name = ‘John Smith’;

tlocal_data.data.user_age = 23;

tlocal_data.flush ()

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

The flush () command is optional, it is used to write the information to the 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:

tstored_user_name = local_data.data.user_name;

tstored_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.

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.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *