flash on 2011-8-17

by tepe
♥0 | Line 93 | Modified 2011-08-19 18:21:36 | MIT License
play

ActionScript3 source code

/**
 * Copyright tepe ( http://wonderfl.net/user/tepe )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/1KV9
 */

package {
    import flash.display.*;
    import flash.text.*
    import flash.events.*;
    import flash.net.*;
    
    public class FlashTest extends Sprite {
        
        private var txt1:TextField = new TextField();
        private var txt2:TextField = new TextField();
        private var so:SharedObject;
        private var dateTime:Date;
        private var date2:Date;
        private var soName:String = "soTest01";
        private var s1:Sprite = new Sprite();
        
        
        public function FlashTest() {
            txt1.text = "aa";
            txt1.x = 0;
            txt1.width = 300;
            txt1.height = 200;
            addChild(txt1);
            txt2.text = "aaa";
            txt2.y = 20;
            //s1.addChild(txt2);
            
            s1.graphics.beginFill(0xdddddd);
            s1.graphics.drawRect(0,0,100,30);
            s1.graphics.endFill();
            s1.y = 300;
            addChild(s1);
            s1.addEventListener(MouseEvent.MOUSE_DOWN,function():void{
                //s1.x += 50;
                if(so.data.count%2 == 0)startTimer();
                else stopTimer();
                //so.clear();
                });


            try {// try : エラー発生予測箇所。この区間でエラーが出ても停止には至らない。エラーが出たらcatchを実行。
                so = SharedObject.getLocal(soName);
                var flushStatus:String = so.flush();
                var now:Date = new Date();
                
                if (flushStatus == SharedObjectFlushStatus.FLUSHED) {//
                    if (so.data.id != null) {//二回目以降
                   
                        var str:String;
                        str = so.data.count.toString();
                        txt1.text = str;
                        txt1.appendText("\nID:"+String(so.data.id));
                        txt1.appendText("\nonTimer : "+so.data.onTimer);
                        dateTime = so.data.date;
                        
                        
                    } else {//初回
                        so.data.count = 0;
                        so.data.totalTime = 0;
                        var today:Date = new Date();
                        
                        //so.data.stTime = today;
                        //so.data.date = today;
                        so.data.id = Math.round(Math.random()*100000);
                        so.data.onTimer = false;
                        txt2.text = "StartTimer";
                        txt1.text = "";
                        txt1.appendText("\nID:"+String(so.data.id));
                        
                    }
                    addEventListener(Event.ENTER_FRAME,onEnter);
                }
            } catch (e:Error) {//tryスコープ内でエラーが発生したとき
                txt1.text = "****";
            }
            //stage.addEventListener(KeyboardEvent.KEY_DOWN, InputChar);    

        }//main
        
        private function onEnter(e:Event):void{
            var i:int;
            
            var now:Date = new Date();
            var msec:int = so.data.totalTime + now.valueOf() - so.data[so.data.count-1].valueOf(); 
            txt1.text = "";
            if(so.data.count%2==0)txt1.appendText(String(Math.round(so.data.totalTime/1000)) );
            else txt1.appendText(String(msec/1000));
            for(i=so.data.count-6;i<so.data.count;i++){
                if(i%2 == 0)txt1.appendText("\nSTART : ");
                else txt1.appendText("\n STOP  : ");
                txt1.appendText(so.data[i].toUTCString());
            }
            txt1.appendText("\n"+so.data.count.toString());
            
            
        }//onEnter
        

        
        private function stopTimer():void{
            var now:Date = new Date();
            try{
                so.data.onTimer = false;
                so.data[so.data.count] = now;
                so.data.totalTime += now.valueOf() - so.data[so.data.count-1].valueOf();
                so.data.count++;
            } catch(e:Error){
                
            }
        }//stopTimer
        
        private function startTimer():void{
            var now:Date = new Date();
            try{
                so.data.onTimer = true;
                so.data[so.data.count] = now;
                so.data.count++;
                
            } catch(e:Error){
                
            }
        }//startTimer
        
    }//class
}//package