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

長門

// forked from yanbaka's あのお方のテキストアニメーションを真似してみる
package
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextFieldType;
	import flash.text.TextFormat;
	import flash.utils.Timer;
	
	[SWF(width = "465", height = "465", backgroundColor = "0xFFFFFF", frameRate = "60")]
	public class Main extends Sprite
	{
		private var _mainTf:TextField;
		private var _inputTf:TextField;
		
		private var _tf:TextField;
		private var _addTf:TextField = new TextField;
		private var _last:String;
		private var _len:uint;
		private var _char:String;
		private var _spd:uint;
		private var _spdCnt:uint;
		private var _cnt:uint;
		
		private var timer:Timer;
		private var interval:uint = 5000;
		
		
		public function Main():void
		{
			var label:TextField = addChild(new TextField()) as TextField;
			label.x = 50;
			label.y = 20;
			label.autoSize = TextFieldAutoSize.LEFT;
			label.text = "ちょっと違うけど中村勇吾さんのテキストアニメーションっぽいやつ。\n5秒ごとに入力されたテキストに書き換わるようにしてます。";
			
			_inputTf = addChild(new TextField()) as TextField;
			_inputTf.x = 50;
			_inputTf.y = 70;
			_inputTf.type = TextFieldType.INPUT;
			_inputTf.maxChars = 20;
			_inputTf.border = true;
			_inputTf.width = 200;
			_inputTf.height = 20;
			_inputTf.text = "YUKI.N > 見えてる?";
			
			_mainTf = addChild(new TextField()) as TextField;
			_mainTf.x = 50;
			_mainTf.y = 95;
			_mainTf.autoSize = TextFieldAutoSize.LEFT;
			setFormat(_mainTf, 20);
			
			timer = new Timer(interval);
			timer.addEventListener(TimerEvent.TIMER, timerHandler);
			timer.start();
			
			// 最初
			yText(_mainTf, _inputTf.text, "_");
		}
		
		private function timerHandler(e:TimerEvent):void
		{
			_mainTf.text = "";
			_addTf.text = "";
			_cnt = 0;
			_spdCnt = 0;
			yText(_mainTf, _inputTf.text, "_");
		}
		
		private function yText(tf:TextField, last:String, char:String, spd:uint=5):void
		{
			_tf = tf;
			_last = last;
			_len = _last.length;
			_char = char;
			_spd = spd;
			
			addEventListener(Event.ENTER_FRAME, animation);
		}
		
		private function animation(e:Event):void
		{
			if(_spdCnt >= _spd)
			{
				_spdCnt = 0;
				_addTf.appendText(_last.charAt(_cnt));
				_cnt++;
				
				if(_cnt >= _len)
				{
					removeEventListener(Event.ENTER_FRAME, animation);
					_char = "";
				}
			}
			else
			{
				_spdCnt++;
			}
			_tf.text = _addTf.text + _char.charAt(getRandomInt(_char.length-1, 0));
		}
		
		private function setFormat(tf:TextField, _size:Number):void
		{
			var format:TextFormat = new TextFormat();
			format.size = _size;
			tf.defaultTextFormat = format;
		}
		
		private function getRandomInt(max:uint, min:uint):uint
		{
			var random:uint = Math.floor(Math.random()*(max-min+1))+min;
			return random;
		}
	}
}