AddThis Social Bookmark Button
 Using Shared Objects in Flash

download source files download source files


What are Shared Objects

Shared Objects are very much like browser cookies, they are used to store a small amount of data on the user machine. This data can be retrieved by the Flash player when the SWF is accessed again at a later time. We could actually say that a shared object is a Flash cookie.

The extension for the shared objects is *.sol and on Windows, local shared-objects are stored in Documents and Settings\userName\Application Data\Adobe\Flash Player\#SharedObjects.

Why using Shared Objects

Shared Objects can be used in many situations, here are a few examples :
- You want to retrieve the highest score for a game between sessions.
- You want to retrieve and display a user name like "Welcome John" between sessions.
- If a long text needs to be entered in a text field and the user suddenly need close down the browser of the browser simply crashed, the data can be retrieved using a shared object (We will study that example in our tutorial)
- You want to be able to retrieve some specific settings without having to re-enter them every time.

We are now going to go into more details about creating a shared object with actionscript. We all experienced the horror of typing a long email and loosing it all in one second after a computer crash. In the following tutorial will are going to create a little form, each time the user type on a key the data will be saved to a shared object like that if anything nasty happened the data will be safely saved into the shared object ready to be retrieved.


Tutorial
1. Create a new FLA

2. Create a basic form (fig1)
- create a multiline input text field an give the name myText to the Instance (fig2)
- You can insert a submit button (the data won't be submitted in our tutorial but it could be sent to a php script or ASP or whatever) called the instance of your button myButton
- You can insert a scrollbar on your text field if necessary.

form fig1 instance name fig2

3. Now that we have created a basic form we want to write some code that will:
- retrieve or create a shared object.
- If the shared object exists it will load the saved data in the text field if not it will create it.
- detect when the user type something new in the text field.
- if something is typed we need to write the data immediately to a shared object.
- when the submit button is pressed we can safely delete the shared object.

To create or retrieve the shared object we use the following syntax :
var myCookie:SharedObject = SharedObject.getLocal("mySavedData");
Above I use strict data typing but you might as well write :
var myCookie = SharedObject.getLocal("mySavedData");
The code above stores a reference to the shared object in the variable myCookie. If the shared object exists the properties of mySavedData (our shared object) are stored in myCookie and can be retrieved. If the shared object doesn't exist it creates it.

continue to next page...
AddThis Social Bookmark Button
If you think this page is providing useful information, don't hesitate to leave a comment.
flashvalley
 
Copyright ©2006-2008 flashvalley.com - All rights reserved