flash on 2010-1-20

by nausicaa
import flash.text.TextFormatAlign;
♥0 | Line 55 | Modified 2010-01-20 15:56:41 | MIT License
play

ActionScript3 source code

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

package {
    import flash.display.Sprite;
    import flash.text.TextField;
    import flash.text.TextFieldAutoSize;
    import flash.text.TextFormat;
    //import flash.text.TextFormatAlign;
    import flash.utils.Timer;
    import flash.events.TimerEvent;
    
    public class MyTextField extends Sprite {
    		public var fld:TextField;
    		public var tf:TextFormat;
    		public var msg:String="Wonderflと\nActionScript3.0\nワンダフルな世界!";
    		public var charPos:uint;
    		public var counter:uint;
    		public var currentMsg:String;
    		public var timer:Timer;
    		public var offset:int=Math.floor(10*Math.random())+5;
    			
        public function MyTextField(){
        		tf = makeTextFormat("_typeWriter",24,0x000000);
        	
        		fld = new TextField();
        		fld.x=100;
        		fld.y=50;
        		fld.width=250;
        		fld.autoSize=TextFieldAutoSize.LEFT;
        		
        		fld.defaultTextFormat=tf;
        		addChild(fld);
        		
        		timer=new Timer(10);
        		timer.addEventListener(TimerEvent.TIMER,timerHandler);
        		timer.start();
        		
        		//fld.selectable=false;
        }
        public function makeTextFormat(font:String, size:uint, color:uint):TextFormat{
        		var tf:TextFormat=new TextFormat();
        		tf.font=font;
        		tf.size=size;
        		tf.color=color;
        		return tf
        }
        public function timerHandler(event:TimerEvent):void{
        		var startCharCode:uint=msg.charCodeAt(charPos)-offset;
        		var char:String=String.fromCharCode(startCharCode+counter);
        		fld.text=msg.substring(0,charPos)+char+"_";
        		
        		if(msg.charAt(charPos)==char){
        			if(charPos==msg.length-1){
        				timer.stop();
        				fld.text=msg;
        			}else{
        				offset=Math.floor(10*Math.random())+5;
        				charPos++;
        				counter=0
        			}
        		}else{
        			counter++;
        		}
        		
        }
    }
}