Accessibility

Table of Contents

Persistent data: Saving user preferences and game scores

Data-saving methods

Data refers to any piece of information that is required by a program to execute. Today, no program or application can function without data. Every data has an associated data type. This data type helps the data to be differentiated from other data types.

Data in Flash Lite shared objects are assigned as attributes. These attributes can have different data types such as Integer, String, Array, Object, Date, Boolean, and much more.

Open dataTypes.fla and go to frame 1. This frame has various data types declared, which would later be bundled into a single shared object (see Figure 3).

Data types bundled into a single shared object

Figure 3. Data types bundled into a single shared object

This code first checks to see if the shared object contains any data attributes using the getSize() function. If it does contain data, then it will copy the values from the data properties of the shared object to the globally defined variables in Flash.

During the execution of the application, globally defined variables in Flash may be appended or altered:

Arr.push({name:nameTxt.text, age:ageTxt.text});
count++;
information = "This is your first visit to this page";

These variables can now be saved in the SharedObject properties:

mySO.data.arr = arr;
mySO.data.count = count;
mySO.data.info = information;

An important point to note is that values cannot be assigned and saved directly to the data properties of a shared object without using the data keyword. Consider the following cases:

mySO.data = someValue wrong
mySO.variableName = someValue wrong
mySO.data.variableName = someValue correct

If you do use SharedObject with properties that are not preceded by the data keyword, the properties will be known as private properties and will be available for use only while the application is running on the device. If the application shuts down, the shared object closes and the private properties are lost.

Data properties can also be deleted from a shared object using the delete keyword. Most developers use the null or undefined keyword to clear the data properties, but these keywords only nullify the values of the properties. They do not actually delete them.