[AS3]テキストがランダムに表示するSHUFFLE TEXT

先日、shuffleTextのJS版を公開したのですが、意外とAS3版を求めて検索してこのBLOGに来る方が多いので、AS3版も公開します。

デモはこちら。

使い方はドキュメントクラスを見ていただければ分かると思います!
因みに、ランダムな文字はfromCharCodeを利用していたのですが、macでは何故か動きが遅いので、事前に文字列を変数に用意して、ランダムな位置の文字を返すようにしてます。

何故かわかる方教えてください!!

ドキュメントクラス

package {
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextField;
	import flash.text.TextFieldAutoSize;
	
	public class Main extends Sprite {
		private var _tFld:TextField;
		private var _shuffleText:ShuffleText;
		private var _btn:Sprite;
		
		public function Main():void {
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void 
		{
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
			
			contentsStart();
		}
		
		private function contentsStart():void {
			_tFld = new TextField();
			_tFld.autoSize = TextFieldAutoSize.LEFT;
			_tFld.text = "abcdefghijklmnopqrstuvxyz"
			_tFld.x = stage.stageWidth / 2 - _tFld.textWidth / 2;
			_tFld.y = 50;
			addChild(_tFld);
			_shuffleText = new ShuffleText(_tFld, "en");
			
			_btn = new Sprite();
			_btn.graphics.beginFill(0);
			_btn.graphics.drawRect(stage.stageWidth / 2 - 50, 100, 100, 50);
			_btn.graphics.endFill();
			addChild(_btn);
			_btn.addEventListener(MouseEvent.MOUSE_OVER, onMouseover);
		}
		
		private function onMouseover(e:MouseEvent):void {
			_shuffleText.shuffle();
		}
		
	}
	
}

ShuffleTextクラス

package {
	import flash.events.Event;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;
	
	/**
	 * ...
	 * @author mellowdown.net
	 */
	public class ShuffleText {

		private var _type:String;
		private var _originallyStr:String;
		private var _fld:TextField;
		private var _showStr:String;
		private var _loopTimer:Timer;
		private var _showTimer:Timer;
		private var _en:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-/^!?&%$#+*"
		private var _hiragana:String = "あいうえおかきくけこさしすせそたちつてとなにぬねのはひふへほらりるれろ";
		private var _katakana:String = "アイウエオカキクケコサシスセソタチツテトナニヌネノハヒフヘホラリルレロ";
		private var _kanji:String = "亜唖娃阿哀愛挨姶逢葵茜穐悪握渥旭葦芦鯵梓圧斡扱宛姐虻飴絢綾鮎或粟袷";
		
		public function ShuffleText(fld:TextField, type:String = "en", loopDelay:uint = 10, showDelay:uint = 50) {
			_fld = fld;
			_originallyStr = _fld.text;
			_type = type;

			_loopTimer = new Timer(loopDelay);
			_loopTimer.addEventListener(TimerEvent.TIMER, onLoop);
			
			_showTimer = new Timer(showDelay);
			_showTimer.addEventListener(TimerEvent.TIMER, onOriginallyStrShow);
		}
		
		public function shuffle():void {
			_showStr = "";
			
			_loopTimer.reset();
			_showTimer.reset();
			
			_loopTimer.start();
			_showTimer.start();
		}
		
		public function dispose():void {
			_loopTimer.removeEventListener(TimerEvent.TIMER, onLoop);
			_showTimer.removeEventListener(TimerEvent.TIMER, onOriginallyStrShow);
			_loopTimer = null;
			_showTimer = null;
		}
		
		private function onLoop(e:TimerEvent):void {
			_fld.text = _showStr;			
			if (_originallyStr.length - _showStr.length > 0) {
				for (var i:int = 0; i < _originallyStr.length - _showStr.length; i++) {
					var shuffleStr:String = getShuffleStr();
					_fld.appendText(shuffleStr);
				}
			} else {
				_loopTimer.reset();
			}
		}
		
		private function getShuffleStr():String {
			var str:String;
			switch (_type) {
				case "en":
					str = _en.charAt(Math.round(Math.random() * (_en.length - 1)));
					//str = String.fromCharCode(33 + Math.round(Math.random() * 99));
					break;
				case "hiragana":
					str = _hiragana.charAt(Math.round(Math.random() * (_hiragana.length - 1)));
					//str = String.fromCharCode(12352 + Math.round(Math.random() * 84));
					break;
				case "katakana":
					str = _katakana.charAt(Math.round(Math.random() * (_katakana.length - 1)));
					//str = String.fromCharCode(12448 + Math.round(Math.random() * 84));
					break;
				case "kanji":
					str = _kanji.charAt(Math.round(Math.random() * (_kanji.length - 1)));
					//str = String.fromCharCode(19968 + Math.round(Math.random() * 200));
					break;
			}
			return str;
		}
		
		private function onOriginallyStrShow(e:Event):void {
			if (_showStr.length < _originallyStr.length) {
				_showStr = _originallyStr.substr(0, _showStr.length + 1);
			} else {
				_showTimer.reset();
			}
		}
	}
	
}

コメント

Comments are closed.

Post Navigation