This site is now just an archive over the rise and fall of Flash. The domain flashmagazine.com is available for sale
Login | Register

Breadcrumbs: Shared Objects

This tutorial will show you how to use the SharedObject, a powerful new feature in Flash MX. The SharedObject can do for Flash what cookies does for the Browser, but much better and simpler. This article assumes basic knowledge of Flash MX and Action Script.

The SharedObject gives Flash the ability to save variables to a user's PC. This allows your Flash Application to remember a user's name, or record data on a user's preferences. Flash will store up to 100k of data, any memory requirement larget than 100k will ask the user to allow or deny more local storage.

In this tutorial we will touch on the SharedObject's below method and property.

1. getLocal - getLocal is used to create a reference to the sharedObject for saving and restoring data. This method takes one parameter, a unique name for the object. When naming, it's a good idea to not only distinguish your company but also distinguish the many flash applications you may have.
i.e. jasonmperry.com/sharedObjectFlashApp
2. data - data is a array of elements currently in the shared object.

This content requires Flash Player 9 (or a more recent version). You need to upgrade your Flash Player



The above example allows you to submit your name and e-mail address into the application (I'm not collecting this data). If you close your browser window and come back you'll notice flash remembered the values you entered.

Getting the SharedObject
To develop our test application we need to create a new Flash file. On the canvas create two text boxes named txtName and txtEmail and a submit button. Feel free to provide labels for your text boxes.

Next we need to open the action script window and enter the below code in the 1st frame of your flash file. This will initiate your shared object with a designated name (this name should always be unique and is only accessible to the domain that created it).

 

//insures code block only runs once
if( init == null )
{
  init = true;
  so = SharedObject.getLocal( "jasonmperry.com/sharedObject" );
 
  //if shared object has data already in it
  if( (so.data[ "name" ] != null) && (so.data[ "email" ] != null) )
  {
    //gets data and stores in text box object
    txtName.Text = so.data[ "name" ];
    txtEmail.Text = so.data[ "email" ];
  }
}



Saving to the SharedObject
Now we need to save the data in the text boxes to our user's pc. To do this create a on( press ) event on your submit button with the below code.

on( press )
{
	//if the text boxes have something in them
	if( (txtName != null) && (txtEmail != null) )
	{
		//get text box content and place to shared object
		so.data[ "name" ] = txtName.Text;
		so.data[ "email" ] = txtEmail.Text;
		
		//don't wait from app to end
		//save now!
		so.flush();
	}
}



This will determine if the text boxes have valid data. If so, it will record the text box data to the SharedObject and immediately store that data with the flush() method. The flush() method is optional; if you don't include it Flash will automatically save to the SharedObject when the application closes.

Conclusion
As you can see the Flash SharedObject is very powerful and allows you to develop intuitive flash applications that remember. Some practical uses for this include:

 

  • Storing a user's preferences like color and music.

  • Caching data to the local computer from web services. This would allow users to immediately view data while the content is refreshed from backend data sources. This could also decrease the number of roundtrips you flash app makes to the server.

  • Auto-adjust a navigation. For example a Flash navigation could auto-adjust to include the current location based on the last button a user pressed.



If you have any questions or comments feel free to contact me at jason@jasonmperry.com and also download the source files here.

More articles by Jason Michael Perry are available at his web site www.jasonmperry.com and the breadcrumbs archive. If you questions or comments you can also e-mail him at Jason@jasonmperry.com.

 

About Jason Michael Perry

Jason Michael Perry is a freelance instructor, developer, and writer. Please check out his new book Breadcrumbs: ActionScript 3 by Smart Ameba Publishing. If you have questions, a topic you would like covered, or more feel free to email him at jason@smartameba.com.

Get new stories first

Click to follow us on Twitter!

 

Comments

No comments for this page.

Submit a comment

Only registered members can comment. Click here to login or here to register