In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

TextField_animation02

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

package {
	import flash.display.Sprite;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFormat;
	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(30 * Math.random()) + 5;
		//コンストラクタ
		public function MyTextField(){
			tf = makeTextFormat("_typewriter",24,0x000000);
			//テキストフィールドの作成
			fld = new TextField();
			fld.x = 50;
			fld.y = 50;
			fld.autoSize = TextFieldAutoSize.LEFT;
			//書式の初期値
			fld.defaultTextFormat = tf;
			addChild(fld);
			//アニメーション用のループを行うタイマー
			timer = new Timer(10);
			timer.addEventListener(TimerEvent.TIMER,timerHandler);
			timer.start();
		}
		//書式の作成
		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;
		}
		//文字コードを探りながら1文字ずつ追加
		public function timerHandler(event:TimerEvent):void {
			//表示したい文字コードよりoffsetだけ小さい文字コード
			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;//文末の "_"を取り去るためにmsgを入れ直します。
				} else {
					//次に表示する文字に移ります。
					offset = Math.floor(30 * Math.random()) + 5;
					charPos++;
					counter = 0;
				}
			} else {
				//次の文字コードに進めます
				counter++;
			}
		}
	}
}