Posted By

allnatural on 03/17/09


Tagged

local SharedObject


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

damarev
thastyle
muoto
quan


Advanced Local SharedObject


 / Published in: ActionScript 3
 

More advanced Local SharedObject class

  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.  
  20. mySo = SharedObject.getLocal("application-name");
  21. output.appendText("SharedObject loaded...\n");
  22. output.appendText("loaded value: " + mySo.data.savedValue + "\n\n");
  23. }
  24.  
  25. private function saveValue(event:MouseEvent):void {
  26. output.appendText("saving value...\n");
  27. mySo.data.savedValue = input.text;
  28.  
  29. var flushStatus:String = null;
  30. try {
  31. flushStatus = mySo.flush(10000);
  32. } catch (error:Error) {
  33. output.appendText("Error...Could not write SharedObject to disk\n");
  34. }
  35. if (flushStatus != null) {
  36. switch (flushStatus) {
  37. case SharedObjectFlushStatus.PENDING:
  38. output.appendText("Requesting permission to save object...\n");
  39. mySo.addEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
  40. break;
  41. case SharedObjectFlushStatus.FLUSHED:
  42. output.appendText("Value flushed to disk.\n");
  43. break;
  44. }
  45. }
  46. output.appendText("\n");
  47. }
  48.  
  49. private function clearValue(event:MouseEvent):void {
  50. output.appendText("Cleared saved value...Reload SWF and the value should be \"undefined\".\n\n");
  51. delete mySo.data.savedValue;
  52. }
  53.  
  54. private function onFlushStatus(event:NetStatusEvent):void {
  55. output.appendText("User closed permission dialog...\n");
  56. switch (event.info.code) {
  57. case "SharedObject.Flush.Success":
  58. output.appendText("User granted permission -- value saved.\n");
  59. break;
  60. case "SharedObject.Flush.Failed":
  61. output.appendText("User denied permission -- value not saved.\n");
  62. break;
  63. }
  64. output.appendText("\n");
  65.  
  66. mySo.removeEventListener(NetStatusEvent.NET_STATUS, onFlushStatus);
  67. }
  68.  
  69. // UI elements
  70. private var inputLbl:TextField;
  71. private var input:TextField;
  72. private var output:TextField;
  73. private var saveBtn:Sprite;
  74. private var clearBtn:Sprite;
  75.  
  76. private function buildUI():void {
  77. // input label
  78. inputLbl = new TextField();
  79. addChild(inputLbl);
  80. inputLbl.x = 10;
  81. inputLbl.y = 10;
  82. inputLbl.text = "Value to save:";
  83.  
  84. // input TextField
  85. input = new TextField();
  86. addChild(input);
  87. input.x = 80;
  88. input.y = 10;
  89. input.width = 100;
  90. input.height = 20;
  91. input.border = true;
  92. input.background = true;
  93. input.type = TextFieldType.INPUT;
  94.  
  95. // output TextField
  96. output = new TextField();
  97. addChild(output);
  98. output.x = 10;
  99. output.y = 35;
  100. output.width = 250;
  101. output.height = 250;
  102. output.multiline = true;
  103. output.wordWrap = true;
  104. output.border = true;
  105. output.background = true;
  106.  
  107. // Save button
  108. saveBtn = new Sprite();
  109. addChild(saveBtn);
  110. saveBtn.x = 190;
  111. saveBtn.y = 10;
  112. saveBtn.useHandCursor = true;
  113. saveBtn.graphics.lineStyle(1);
  114. saveBtn.graphics.beginFill(0xcccccc);
  115. saveBtn.graphics.drawRoundRect(0, 0, 30, 20, 5, 5);
  116. var saveLbl:TextField = new TextField();
  117. saveBtn.addChild(saveLbl);
  118. saveLbl.text = "Save";
  119. saveLbl.selectable = false;
  120.  
  121. // Clear button
  122. clearBtn = new Sprite();
  123. addChild(clearBtn);
  124. clearBtn.x = 230;
  125. clearBtn.y = 10;
  126. clearBtn.useHandCursor = true;
  127. clearBtn.graphics.lineStyle(1);
  128. clearBtn.graphics.beginFill(0xcccccc);
  129. clearBtn.graphics.drawRoundRect(0, 0, 30, 20, 5, 5);
  130. var clearLbl:TextField = new TextField();
  131. clearBtn.addChild(clearLbl);
  132. clearLbl.text = "Clear";
  133. clearLbl.selectable = false;
  134. }
  135. }
  136. }

Report this snippet  

You need to login to post a comment.