Skip to content

Instantly share code, notes, and snippets.

@grapefrukt
Created December 15, 2013 17:57
Show Gist options
  • Save grapefrukt/7975990 to your computer and use it in GitHub Desktop.
Save grapefrukt/7975990 to your computer and use it in GitHub Desktop.
package ;
import flash.errors.Error;
import flash.net.SharedObject;
/**
* Completely unnecessary wrapper for SharedObject
* @author Martin Jonasson, m@grapefrukt.com
*/
class SaveData {
private var _so:SharedObject;
public function new() {
// maybe call load here, depending on use case...
}
public function load():Void {
// first part is the name of your project or otherwise
// use a slash as the second argument to make it location independent
// (if not .swf's stored in different locations will have separate saves)
_so = SharedObject.getLocal( "spaceblocks", "/" );
}
// saving, put this in the set() call to always save when something changes
public function save():Void {
try {
var flushStatus = _so.flush();
} catch ( e:Dynamic ) {
trace("Couldn't write...");
}
}
// saves a variable (can be anything pretty much, including objects and such)
public function set(field:String, data:Dynamic):Void {
Reflect.setField(_so.data, field, data);
}
// what this function does is left as an excersise for the reader ;)
public function get(field:String):Dynamic {
return Reflect.field(_so.data, field);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment