Accessibility

Table of Contents

Persistent data: Saving user preferences and game scores

What are Flash Lite shared objects?

The SharedObject class in Flash Lite allows a user to save and retrieve local data from a device. The data the user may save is mostly application-specific information such as application state, high score, user preference, user name, and so on. This helps make the application more robust and interactive for the end user. In other words, if you are familiar with the desktop development of Flash, you could compare Flash Lite shared objects to browser cookies.

Shared objects are located in the system directory of a mobile phone (typically C:\System\Apps\saflash2\) and they have their own format.

Figure 1 shows a very simple example of a Flash Lite shared object. It maintains simply the number of visits you have made to the application. Every time you open this application in the Flash viewer on the mobile device, the count increments by one. Figure 1 shows is a representation of the final application. You can download the tutorial files and study the code in the file named visits.fla.

Flash Lite shared object application that counts the number of times a user accesses it

Figure 1. Flash Lite shared object application that counts the number of times a user accesses it

Shared objects maintain local persistence. The basic steps for using a shared object on a mobile device is by creating one using the syntax SharedObject.getLocal():

var mySO:SharedObject = SharedObject.getLocal("visits");
SharedObject.addListener("visits", soInitHandler);

In the above code I have created an object named mySO, which is associated with a shared object on the device named visits. It is important to note that unlike the desktop version of the shared object, the mobile version does not allow sharing of data between different Flash movie instances. It is also a good practice to attach a listener to a shared object because data loading is slow on devices. With listeners, you can make sure that the data will be available to you only when it is ready.

The shared object is ineffective if some data is not stored into it. So I create a data attribute property called count and set the value to 1. You can add as many data attributes as your device storage can hold for the application:

mySO.data.count = 1;

The flush option is optional. You would ideally use this option explicitly to flush, or write, your shared object to the device immediately:

mySO.flush();

When you flush a data attribute, it always returns a Boolean value of true/false or a string value of pending. If you open the second demo file, maxSizeError.fla, you will notice that it displays the flush status (see Figure 2).

The display after performing the flush

Figure 2. The display after performing the flush

In frame 2 of the file, I perform a flush immediately after setting the value of the data attribute. I again perform a flush on frame 5 to see the final status. I do this because the flush status is asynchronous. This means that the writing of data to the shared object does not happen immediately.

I will not go into further details of the flush function because you can find more information about it in the Flash Lite 2.0 CDK or the Help system from within the Flash authoring environment.

In the second demo file, I have also used the following two SharedObject functions:

  • SharedObject.getSize();
  • SharedObject.getMaxSize();

The getSize() function gets the current size of the shared object in bytes. The getMaxSize() function gets the total size of the shared object reserved for all the movies combined on the device.

The getSize() function is important especially if you are writing the shared object for a movie for the first time. You can use it to check and define a number of data attributes that you may want to write for the first time only.