Shared Object
Tutorial
        by Jeremy Moseley : 21 July 2004

This tutorial will teach you how to use the Shared Object in Actionscript 2.0. The Shared Object is like a cookie, but it does not require a browser or server-side scripts to use. It is for Flash only and can be loaded instantaneously from the users machine. It is the most efficient way to save information about your users so that they can use it at a later date.

Here is an example using the Shared Object:

[ This is an example of the Shared Object ]

Here is How
These steps will show you how to easily use the Shared Object.

  1. First, create or open a Flash document.

  2. Now open the Actions Panel for the first frame (or whenever you want to load the data)

  3. Create a Shared Object using the getLocal method:

user_so = SharedObject.getLocal("user");
//user_so is the name of our sharedObject that we are creating. this can be any name
//"user" is the actual name of the file we are loading from the clients harddrive.
//This can be anything, but you must use the same name to get the same data
  1. Now that we have our Shared Object we will check to see if there is data already in it:

if(user_so.data.tname != undefined) {
//data exists
//do actions
} else {
//data does not exists
//do actions
}
  1. If there is no data we can easily insert it by assiging values to the .data property:

user_so.data.tname = "insert value here";
user_so.data.address = "insert value here";
user_so.data.other = "insert value here";
//user_so.data actually holds the information gotten from the user.
//easily store more than one value per object simply by assigning them names and values
//user_so.data.tname is a value from the Shared Object.
//.tname can be anything and is simply used to reference the value
  1. If there is data we can access it by simply using an assignment command:

name_txt.text = user_so.data.tname;
address_txt.text = user_so.data.address;
other_txt.text = user_so.data.other;
//name_txt is just a dynamic text field that will display our data
//you can load data from an Shared Object as soon as it has been assigned
//example:
user_so.data.tname = "this is my name";
user_name = user_so.data.tname;
trace(user_name);
//this will return "this is my name"
  1. If you would like to store information in the Shared Object but not have is stored, assign it as another property of the Object:

user_so.string = "this won't be saved";
user_so.another = "neither will this";
//neither of the values above will be saved
//Flash only saves the values that are in the Shared Object.data property
  1. To delete a Shared Object just use the clear method:

user_so.clear();
//this will delete the Shared Object
//after you call this command the Shared Object will no longer exist
  1. You do not need to worry about saving the information as Flash will do this automatically, however you can still use the flush method:

user_so.flush();
//this will save the info in the user_so object
//however Flash will still save changes automatically when it closes

Back Track
Well done for making it this far. Here we have a little review.

  1. First, you Learned to create a Shared Object using the getLocal method.

  2. You learned to check for data using an if statement.

  3. You learned to add and delete data.

  4. You learned to delete the Shared Object with the clear method.

  5. Finally, you learned to save the shared Object with the flush method.

I have included the source for the example above in this .ZIP file:

Download ZIP

I hope this tutorial has helped you out and will allow you to create better and more badass things in Flash in the future. Remember, if you have any problem at all you can always post at the KirupaForums and somebody (maybe even me) should be able to give you a hand. Thanks for reading.

Have Fun!

Jeremy Moseley - Jerez_z
My Forum

 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.