Using AS3 SharedObject in Flash

739 views

Shared Objects are very similar to cookies on a browser, however, they are not stored with the rest of your browsers cookies and are not deleted when the user deletes the regular cookies, instead they are completed managed by the Flash Player.

1.Basic Usage of the SharedObjects Class

In order for your data to be stored inside a Shared Object the following must be done:

1.Create an instance of the SharedObjects Class using the getLocal() method.
2.Store data inside the SharedObjects instance.
3.Write the ShareObjects instance into the player using the flush() method.

var mySharedObject:SharedObject = SharedObject.getLocal("republicofcode");
mySharedObject.data.firstName = "John";
mySharedObject.data.lastName = "Doe";
mySharedObject.flush();

2.Retrieving Data Stored in a SharedObject

Retrieving the data stored as in a Shared Object is very similar to the process for storing this data. This requires only two steps, the first is retrieving the actual Shared Object by using the getLocal() method and then retrieving the data through the data property.

We have earlier said that using the getLocal() method is a special process that attempts to check if your Shared Object exists before attempting to create one. If that object does exist, then it simply retrieves it and stores it in your variable:
Once you retrieve the SharedObject, you can dig up the content within it by directly accessing the information through the data property:

var mySharedObject:SharedObject = SharedObject.getLocal("republicofcode");
trace(mySharedObject.data.firstName);
trace(mySharedObject.data.lastName);

3. Deleting Data

If for some reason you want to delete all the data stored in your Shared Object, you can do that by using the clear() method:
var mySharedObject:SharedObject = SharedObject.getLocal("republicofcode");
mySharedObject.clear();

Post a new comment


You need to be logged in in order to post a comment. Please click here to go login and be redirected to the post form.