Here's my code which uses the SharedObject to implement a User Data Manager. This is my first ActionScript of any usefulness.

ja
http://vispo.com

package
{
// This is for managing the user's state from session to session.
// This saves their preferences and current state.

import flash.net.SharedObject;
import flash.net.URLRequest;
import flash.net.navigateToURL;


public class UserDataMgr
{
 protected var _version:String= "1.5";
// The version of this program. The string is included in _userDataFilename.
 protected var _userDataFileName:String = "Main_" + _version;
 // Customize this to set the name of the file stored on disk.
 protected var _sharedObj:SharedObject;

 public function UserDataMgr()
 {
  //The constructor of the UserDataMgr class.
  readPrefs();
 }

 protected function readPrefs():void
 {
  // Called only by the constructor.
  // At the end of this method, _sharedObj is valid or we've exited
  // the program. It's either the previously stored info or, if
  // none was found on disk, then it's default user data.
  try {
   _sharedObj = SharedObject.getLocal(_userDataFileName);
   if (_sharedObj.size == 0)
   {
    // Then fill it with default data.
    // This happens when the app is first run.
    setDefaultUserData();
   }
  }
  catch(e:Error) {
   // Shared Object could not be created.
javaScriptPopUp("This program cannot run properly if you do not allow it to save data to your hard disk. The program will now close.");
   closeApp();
  }
 }


 protected function closeApp():void
 {
  // Called by readPrefs.
  // This is supposed to close the app.
// This probably doesn't work for AIR apps and who knows what other types of Flash apps. // This code should check to see if we're in an AIR app (etc), and act accordingly.
  // For more info, search CHC for 'close program'
  var urlString:String = "javascript:window.opener = self; self.close();";
  var request:URLRequest = new URLRequest(urlString);
  navigateToURL(request, "_self");
 }

 protected function javaScriptPopUp(s:String):void
 {
  // Called by readPrefs.
  // This opens a JavaScript pop up alert that displays s.
  var arg:String="javascript:alert(\'" + s + "\')"
  var alert:URLRequest = new URLRequest(arg);
  navigateToURL(alert, "_self");
 }

 protected function setDefaultUserData():void
 {
  // Sets the default User Data in the SharedObject type.
  setProperty("userId", "testId");
  setProperty("userNumbers", {one:Math.random(), two:Math.random()});
  setProperty("bobo", 3.0);
 }

 public function setProperty(name:String, value:*):void
 {
  // Call this from outside the object to set a property.
  _sharedObj.data[name]=value;
 }

 public function getProperty(name:String)
 {
  // Call this from outside the object to get a property.
  return _sharedObj.data[name];
 }

}
_______________________________________________
Flashcoders mailing list
[email protected]
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to