▼ActionScript 3.0メモ▼
ローカル領域へのデータ保存


ローカル領域へのデータ保存を行うFlashを作成する。


ソースコード
SharedObjectEx.as
package {
    import flash.display.*;
    import flash.text.*;
    import flash.events.*;
    import flash.net.*;
   
    //ローカル領域へのデータ保存
    public class SharedObjectEx extends Sprite {
        private var textField:TextField;//テキストフィールド
        private var so:SharedObject;    //SharedObject
        
        //コンストラクタ
        public function SharedObjectEx() {
            //画面の描画
            var child:Sprite = new Sprite();
            child.graphics.beginFill(0xffffff);
            child.graphics.drawRect(0,0,240,240);
            child.graphics.endFill();
            addChild(child);

            //テキストフィールドの追加
            textField=addTextField(child,"");
            textField.x=10;
            textField.y=10;

            //ボタンの追加
            addButton(child,"書き込み",writeHandler,10,36);
            addButton(child,"読み込み",readHandler,70,36);

            //SharedObjectの取得
            so=SharedObject.getLocal("SharedObjectEx");
            if (so.data.text!=null) textField.text=so.data.text;            
        }

        //テキストフィールドの追加
        private function addTextField(doc:DisplayObjectContainer,
            text:String):TextField {
            var textField:TextField=new TextField();
            doc.addChild(textField);
            textField.width=200;
            textField.height=16;
            textField.text      =text;//テキスト
            textField.selectable=true;//選択可
            textField.border    =true;//ボーダー
            textField.type      =TextFieldType.INPUT;//入力
            return textField;
        }

        //ボタンの追加
        private function addButton(doc:DisplayObjectContainer,
            text:String,handler:Function,x:int,y:int):TextField {
            var button:TextField=new TextField();
            doc.addChild(button);
            button.text           =text;                  //テキスト
            button.autoSize       =TextFieldAutoSize.LEFT;//オートサイズ
            button.selectable     =false;                 //選択不可
            button.border         =true;                  //ボーダー
            button.background     =true;                  //背景色
            button.backgroundColor=0xdddddd;
            button.addEventListener(MouseEvent.CLICK,handler);
            button.x=x;
            button.y=y;
            return button;
        }

        //書き込み
        private function writeHandler(evt:MouseEvent):void {
            so.data.text=textField.text;
            so.flush();
        }
        
        //読み込み
        private function readHandler(evt:MouseEvent):void {
            if (so.data.text!=null) textField.text=so.data.text;            
        }
    }
}



−戻る−