dyadica.net

Fun and Games in the Dyadic Sea

Introduction to Flash Shared Objects

So whats a Shared Object

Flash “Shared Objects” provide facility to store and retrieve information on a clients machine similar in method to conventional to cookies. Examples of use for Shared Object data could be a user’s name, progress and high score. Shared objects are stored in .sol files located in the Flash player directory of the user’s profile e.g.:

“C:/Documents and Settings/Administrator/ApplicationData/Macromedia/Flash Player”

Creating a Shared Object

Before we can use a shared object, we have to create a reference to it within our Flash movie. To do so, just put this line of code in the first frame of your movie:

save_data = SharedObject.getLocal("user_data");

This line creates an Object named “save_data” which is associated with a shared object on the local hard-drive named user_data. Please note that in, this data can also be read from other movies from the same domain that created the hence “Shared” Object.

Writing Data to the Object

Writing data to the object s an easy task.  Take a look at the following example to save a users game progress:

save_data.data.game_level = 2;
save_data.data.game_score = 50;
save_data.data.game_health = 50;

Note that this code must be in the same level where you have created the Shared Object.Each of the lines can be broken down as follows:

“Shared Object” .data. “Reference Name” = value

To write the information to disk immediately you can use the optional flush() command. By default Flash writes the shared object to a file when the SWF movie is closed or when the shared object is garbage-collected as it no longer has any references. The code for the flush()command is as follows:

save_data.flush()

Reading Data from the Object

To retrieve data from a saved Shared Object is similar to the writing method, just reverse the calls as follows:

var game_level = save_data.data.game_level;
var game_score = save_data.data.game_score;
var game_health = save_data.data.game_health;

To perform a read you must have first have created a reference to the Shared Object. However as we have already done this in our case we don’t need to in this instance. However if we wanted to call it in another movie we would just add:

save_data = SharedObject.getLocal("user_data");

as before. Please note, the definition must come before the retreval calls and at same level.

Some Additional Information

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

Shared Objects 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 them as a bonus feature.

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 that the stage size of your movie is at least this is the minimum size Flash requires to display the dialog box to allow for both of the above modifications.

Cats: Uncategorized

page meta

get trackback image get trackback image
details
  • Posted: Wednesday, 21 January '09
  • Edited: Wednesday, 21 January '09
  • Author: admin
css | xhtml | dyadica.net © admin 2010 | powered: wordpress | fuel: ps-wii-elite | top | sign-up