1 package { 2 import flash.display.Sprite; 3 import flash.events.MouseEvent; 4 import flash.events.NetStatusEvent; 5 import flash.net.SharedObject; 6 import flash.net.SharedObjectFlushStatus; 7 import flash.text.TextField; 8 import flash.text.TextFieldAutoSize; 9 import flash.text.TextFieldType; 10 11 public class SharedObjectExample extends Sprite { 12 13 private var mySo:SharedObject; 14 15 public function SharedObjectExample() { 16 buildUI(); 17 saveBtn.addEventListener(MouseEvent.CLICK, saveValue); 18 clearBtn.addEventListener(MouseEvent.CLICK, clearValue); 19 //默认路径是根据swf的路径来决定的,路径上不要用中文 20 //比如swf地址:E:/testSharedObject/test.swf,那么默认路径就是win7系统 21 //C:/Users/Administrator/AppData/Roaming/Macromedia/Flash Player/#SharedObjects/CZ3BWEGE/localhost/
testSharedObject/test.swf/application-name.sol 22 //路径并不是可以任意指定的,只能在默认的共享路径上选择 23 //上面例子可选路径有: 24 // "/" 25 // "/testSharedObject" 26 // "/testSharedObject/test.swf" 27 //而 "/" 由于与swf路径无关(即使swf移动了位置),所以当作共享数据的路径 28 mySo = SharedObject.getLocal("application-name"); 29 output.appendText("SharedObject loaded... "); 30 output.appendText("loaded value: " + mySo.data.savedValue + " "); 31 } 32 33 private function saveValue(event:MouseEvent):void { 34 output.appendText("saving value... "); 35 mySo.data.savedValue = input.text; 36 37 var flushStatus:String = null; 38 try { 39 flushStatus = mySo.flush(10000);//必须分配给此对象的最小磁盘空间(以字节为单位) 40 } catch (error:Error) { 41 output.appendText("Error...Could not write SharedObject to disk "); 42 } 43 if (flushStatus != null) { 44 switch (flushStatus) { 45 //用户允许来自此域的对象进行本地信息存储,但所分配的空间量不足以存储该对象 46 case SharedObjectFlushStatus.PENDING: 47 output.appendText("Requesting permission to save object... "); 48 mySo.addEventListener(NetStatusEvent.NET_STATUS, onFlushStatus); 49 break; 50 //共享对象已成功写入本地磁盘上的文件 51 case SharedObjectFlushStatus.FLUSHED: 52 output.appendText("Value flushed to disk. "); 53 break; 54 } 55 } 56 output.appendText(" "); 57 } 58 59 private function clearValue(event:MouseEvent):void { 60 output.appendText("Cleared saved value...Reload SWF and the value should be "undefined". "); 61 delete mySo.data.savedValue; 62 } 63 64 private function onFlushStatus(event:NetStatusEvent):void { 65 output.appendText("User closed permission dialog... "); 66 switch (event.info.code) { 67 case "SharedObject.Flush.Success": 68 output.appendText("User granted permission -- value saved. "); 69 break; 70 case "SharedObject.Flush.Failed": 71 output.appendText("User denied permission -- value not saved. "); 72 break; 73 } 74 output.appendText(" "); 75 76 mySo.removeEventListener(NetStatusEvent.NET_STATUS, onFlushStatus); 77 } 78 79 // UI elements 80 private var inputLbl:TextField; 81 private var input:TextField; 82 private var output:TextField; 83 private var saveBtn:Sprite; 84 private var clearBtn:Sprite; 85 86 private function buildUI():void { 87 // input label 88 inputLbl = new TextField(); 89 addChild(inputLbl); 90 inputLbl.x = 10; 91 inputLbl.y = 10; 92 inputLbl.text = "Value to save:"; 93 94 // input TextField 95 input = new TextField(); 96 addChild(input); 97 input.x = 80; 98 input.y = 10; 99 input.width = 100; 100 input.height = 20; 101 input.border = true; 102 input.background = true; 103 input.type = TextFieldType.INPUT; 104 105 // output TextField 106 output = new TextField(); 107 addChild(output); 108 output.x = 10; 109 output.y = 35; 110 output.width = 250; 111 output.height = 250; 112 output.multiline = true; 113 output.wordWrap = true; 114 output.border = true; 115 output.background = true; 116 117 // Save button 118 saveBtn = new Sprite(); 119 addChild(saveBtn); 120 saveBtn.x = 190; 121 saveBtn.y = 10; 122 saveBtn.useHandCursor = true; 123 saveBtn.graphics.lineStyle(1); 124 saveBtn.graphics.beginFill(0xcccccc); 125 saveBtn.graphics.drawRoundRect(0, 0, 30, 20, 5, 5); 126 var saveLbl:TextField = new TextField(); 127 saveBtn.addChild(saveLbl); 128 saveLbl.text = "Save"; 129 saveLbl.selectable = false; 130 131 // Clear button 132 clearBtn = new Sprite(); 133 addChild(clearBtn); 134 clearBtn.x = 230; 135 clearBtn.y = 10; 136 clearBtn.useHandCursor = true; 137 clearBtn.graphics.lineStyle(1); 138 clearBtn.graphics.beginFill(0xcccccc); 139 clearBtn.graphics.drawRoundRect(0, 0, 30, 20, 5, 5); 140 var clearLbl:TextField = new TextField(); 141 clearBtn.addChild(clearLbl); 142 clearLbl.text = "Clear"; 143 clearLbl.selectable = false; 144 } 145 } 146 }