ランダムなアレ

by otias
テストに出る系。。。
*ランダムな文字列から徐々に指定した文字列に揃っていく。
*汚くてごめんなさい!!
*一旦ストップしたあとはクリックでstart/stopです。
♥2 | Line 90 | Modified 2010-02-23 23:54:01 | MIT License
play

ActionScript3 source code

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

/*
*テストに出る系。。。
*ランダムな文字列から徐々に指定した文字列に揃っていく。
*汚くてごめんなさい!!
*一旦ストップしたあとはクリックでstart/stopです。
*/

package {
	import flash.display.Sprite;
	import flash.text.*;
	import flash.events.*;
	import flash.utils.*;
	
	public class RandomChars extends Sprite {
		private var _intervalId:int;
		private var _timeoutId:int;
		private var _timer:Timer;
		private var _count:int = 0;
		private var _txtFld:TextField;
		private var _txtFmt:TextFormat;
		private var _str:String = "hello wonderfl!";
		private var _isSetText:Boolean = false;
		
		public function RandomChars() {
			init();
		}
		
		private function init():void {
			_txtFmt = new TextFormat();
			_txtFld = new TextField();
			with(_txtFmt) {
				align = TextFormatAlign.LEFT;
				bold = true;
				color = 0xEC279E;
				size = 40;
				font = "Arial";
			}
			
			with(_txtFld) {
				defaultTextFormat = _txtFmt;
				background = false;
				border = false;
				autoSize = TextFieldAutoSize.LEFT;
				text = setLength(_str);
				x = 95;
				y = 200;
			}
			addChild(_txtFld);
			
			_timeoutId = setTimeout(switchText, 1000);
			
			_timer = new Timer(33);
			_timer.addEventListener(TimerEvent.TIMER, onLoop, false, 0, true);
			_timer.start();			
			stage.addEventListener(MouseEvent.CLICK, onClick, false, 0, true);
		}
		
		private function onLoop(e:TimerEvent):void {
			for(var i:int=_count; i<_txtFld.length; i++) {
				_txtFld.replaceText(i, i + 1, String.fromCharCode(20 + Math.random() * 106));
			}
			
			if(_isSetText) {
				_txtFld.replaceText(_count, _count + 1, _str.charAt(_count));
				if(_count >= _str.length) {
					_count = _str.length;
					clearInterval(_intervalId);
					_timer.stop();
				}
			} else if(!_isSetText && _count <= 0) {
				_count = 0;
				clearInterval(_intervalId);
			}
		}
		
		private function setLength(str:String):String {
			var initString:String = "";
			for(var i:uint=0; i<str.length; i++) {
				initString += String.fromCharCode(20 + Math.random() * 106);
			}
			return initString;
		}
		
		private function onClick(e:MouseEvent):void {
			switchText();
		}
		
		private function switchText():void {
			if(_timeoutId) {clearTimeout(_timeoutId);}
			if(!_isSetText) {
				_isSetText = true;
				_count = 0;
				_intervalId = setInterval(inc, 60);
			} else if(_isSetText) {
				_isSetText = false;
				_count = 11;
				_intervalId = setInterval(dec, 60);
				_timer.start();
			}
		}
		
		private function dec():void {
			_count--;
		}
		
		private function inc():void {
			_count++;
		}
	}
}