Local shared objects 很类似于浏览器中cookies,LSOs 的功能也和cookies 很类似,如存储用户
登陆网站的用户名,这样不必每次登陆都要输入用户名了,不过LSOs 的功能不仅限于此,它
比cookies 要强大的多,他们不必在客户端和服务器端进行传输,且都是以ActionScript 数据类
型形式存储。与此对应的服务端有remote shared objects (RSOs), LSOs 不需要在客户端和服务
端额外的软件即可工作。
创建或打开共享对象
LSOs 的静态方法getLocal( ) 用于 创建或打开共享对象
var example:SharedObject = SharedObject.getLocal( "example" );
getLocal( )方法首先试图定位找到LSO,如果找不到则根据这个名字创建新的,否则则返回SharedObject 实例
写入数据到共享对象上
example.data.variable = "This is the correct way to store data.";
可以直接存储ActionScript原生数据类型:
example.data.exampleArray = new Array( "a", "b", "c" );
example.data.exampleDate = new Date();
example.data.exampleObject = { key1: "example", key2: -12, key3: null };
但需要注意的是,不能存储可视化对象(例如MovieClips,Sprite,Buttons,TextFields)
保存本地共享对象
这里有个例子调用flush( )保存数据,处理可能返回的结果
var example:SharedObject = SharedObject.getLocal( "example" ); example.data.someData = "a value"; try { var flushResult:String = example.flush( ); // If the flush operation is pending, add an event handler for // netStatus to determine if the user grants or denies access. // Otherwise, just check the result. if ( flushResult == SharedObjectFlushStatus.PENDING ) { // Add an event handler for netStatus so we can check if the user // granted enough disk space to save the shared object. Invoke // the onStatus method when the netStatus event is raised. example.addEventListener( NetStatusEvent.NET_STATUS, onStatus ); } else if ( flushResult == SharedObjectFlushStatus.FLUSHED ) { // Saved successfully. Place any code here that you want to // execute after the data was successfully saved. } } catch ( e:Error ) { // This means the user has the local storage settings to 'Never.' // If it is important to save your data, you may want to alert the // user here. Also, if you want to make it easy for the user to change // his settings, you can open the local storage tab of the Player // Settings dialog box with the following code: // Security.showSettings( SecurityPanel.LOCAL_STORAGE );. } // Define the onStatus() function to handle the shared object's // status event that is raised after the user makes a selection from // the prompt that occurs when flush( ) returns "pending." function onStatus( event:NetStatusEvent ):void { if ( event.info.code == "SharedObject.Flush.Success" ) { // If the event.info.code property is "SharedObject.Flush.Success", // it means the user granted access. Place any code here that // you want to execute when the user grants access. } else if ( event.info.code == "SharedObject.Flush.Failed" ) { // If the event.info.code property is "SharedObject.Flush.Failed", it // means the user denied access. Place any code here that you // want to execute when the user denies access. } // Remove the event listener now since we only needed to listen once example.removeEventListener( NetStatusEvent.NET_STATUS, onStatus ); };
从共享对象中读取数据
// Read the value of exampleProperty from the shared object, // example, and display it in the Output window. trace( example.data.exampleProperty ); 通过读写数据,我们可以判定用户是不是头一次访问swf文件: // Create a shared object and store some data in it var example:SharedObject = SharedObject.getLocal( "example" ); if ( example.data.previouslyViewed ) { // The user has already viewed the .swf file before, perhaps // we skip an introductory help screen here. } else { // This is the first time the user is viewing the .swf file // because previouslyViewed has not yet been set to true. // Set previouslyViewed to true so that the next time this // code is run we know the user has been here before. example.data.previouslyViewed = true; example.flush( ); }
删除共享对象中保存的数据
// 这语句编译通过,但是并不是我们所期望的
example.data.someVariable = null;
因为共享对象中null和undefined都是有效的值,因此上面的代码并没有删除someVariable属性
正确的方法是使用delete命令,如:
// Remove someVariable from the example shared object.
delete example.data.someVariable;
clear( )方法删除整个共享对象,实际上就是删除硬盘中的.sol文件,看下面的代码:
// Create a shared object and store some data in it
var example:SharedObject = SharedObject.getLocal( "example" );
example.data.someData = "a value";
// Displays: a value
trace( example.data.someData );
// Remove the shared object from the disk
example.clear( );
// Displays: undefined
trace( example.data.someData );
需要注意的地方是清除数据后,共享对象的引用仍然是有效的,这是还是可以重新添加数据进行保存。
序列化自定义类
package model { public class Person { private var _firstName:String; private var _age:int; public function Person(firstName:String, age:int) { _firstName = firstName; _age = age; } public function toString( ):String { return _firstName + " is " + _age + " years old"; } } }
编写主类读取和写入数据
package { import flash.net.registerClassAlias; import flash.net.SharedObject; import model.Person; public class Example { public function Example( ) { // Map "model.Person" to the Person class registerClassAlias( "model.Person", Person ); // Create a shared object and store a Person instance in it var example:SharedObject = SharedObject.getLocal( "example" ); // Test to see if the person instance has been saved already if ( example.data.person == undefined ) { trace( "first time, saving person instance" ); var person:Person = new Person("Darron", 24); // Write the class instance to the local shared object example.data.person = person; } else { trace( "person instance already saved, using stored values" ); } /* Every time this code is executed, the following is displayed: Darron is 24 years old */ trace( example.data.person.toString( ) ); }
这里需要注意的是registerClassAlias( )必须在SharedObject.getLocal( )方法之前调用才有效。否则
的话共享对象会把Person解释为普通的object类型进行存储。
《cookbook》