Here is a simple tutorial on using prebuilt AS3 components and SharedObject.
Class soExample:
package {
import fl.controls.Button;
import fl.controls.Label;
import fl.controls.TextInput;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.net.SharedObject;
public class soExample extends Sprite {
private var getData_btn:Button;
private var setData_btn:Button;
private var delData_btn:Button;
private var titleLbl:Label;
private var statLbl:Label;
private var soText:TextInput;
private var _mc:Sprite;
private var mySO:SharedObject;
public function soExample(_mc:Sprite) {
this._mc = _mc;
updateUI();
updateDisplay();
getSO();
}
private function updateUI():void {
getData_btn = createButton(getData_btn, "Get Shared Object", 150, 10, 100, "get_btn");
setData_btn = createButton(getData_btn, "Store Shared Object", 150, 10, 130, "set_btn");
delData_btn = createButton(getData_btn, "Delete Shared Object", 150, 10, 160, "del_btn");
titleLbl = createLabel(titleLbl, "<b>AS3 - Shared Object Example</b>", 200, 10, 10);
statLbl = createLabel(statLbl, "", 400, 10, 200);
soText = createTextInput(soText, 200, 10, 50);
}
private function updateDisplay():void {
_mc.addChild(titleLbl);
_mc.addChild(soText);
_mc.addChild(getData_btn);
_mc.addChild(setData_btn);
_mc.addChild(delData_btn);
_mc.addChild(statLbl);
}
private function createButton(btn:Button, lbl:String, w:uint, x:uint, y:uint, nme:String):Button {
btn = new Button();
btn.label = lbl;
btn.width = w;
btn.move(x, y);
btn.name = nme;
btn.addEventListener(MouseEvent.CLICK, clickHandler);
return btn;
}
private function createLabel(lblRef:Label, lbl:String, w:uint, x:uint, y:uint):Label {
lblRef = new Label();
lblRef.htmlText = lbl;
lblRef.width = w;
lblRef.move(x, y);
return lblRef;
}
private function createTextInput(tiRef:TextInput, w:uint, x:uint, y:uint):TextInput {
tiRef = new TextInput();
tiRef.htmlText = “”;
tiRef.width = w;
tiRef.move(x, y);
return tiRef;
}
private function clickHandler(event:MouseEvent):void {
respondToMouseEvent(event.target.name);
}
private function respondToMouseEvent(nme:String):void {
switch (nme) {
case "get_btn":
getSO();
break;
case "set_btn":
setSO();
break;
case "del_btn":
deleteSO();
break;
}
}
private function getSO():void {
soText.text = "";
mySO = SharedObject.getLocal("userData");
if (mySO.size == 0) {
// Shared object doesn’t exist.
statLbl.htmlText = "Status: SharedObject not found.";
} else {
statLbl.htmlText = "Status: SharedObject value is - <b>"+mySO.data.userName+"</b>";
}
}
private function setSO():void {
if (soText.text != "") {
mySO.data.userName = soText.text;
mySO.flush();
}
getSO();
}
private function deleteSO():void {
mySO.clear();
getSO();
}
}
}
Now, create object of soExample and pass movieclip reference (in which all the components will be added at runtime) as parameter:
var mySoExample:soExample = new soExample(this);
Note: Be sure to add “Button”, “Label” & “TextInput” components to the library of that movie.
Publish and test the flash file. Simple isn’t it




[...] 3: Serializing Classes using registerClassAlias In previous article “Component and SharedObject Example” we’ve seen how to store simple variables in Local Shared Object. But, what if you want to [...]
By: ActionScript 3: Serializing Classes using registerClassAlias « Tushar Wadekar on July 10, 2007
at 5:47 am
Nice blog. I have one similiar to it. Yours is a little more advanced though.
By: Mike on June 15, 2009
at 2:39 pm