yugop先生のアレを作ってみました

※2010年1月1日追記
本エントリーアップ後に確認したところ、WindowsとMACで処理処理速度が大幅に異なることが確認されました。こちら改めてレポート致しますので少々お待ちくださいませ。
(本エントリーに関する内容に変更はありません)

先日のTimerクラスのパフォーマンステストを兼ねて、Yugop先生のテキストがランダムに変化して戻るアレを作ってみました。偶然にも同じようなクラスを作っていた@fumix氏に数時間差で遅れを取ってしまったのは内緒。テスト自体はこのあと行うのですが、何とか実装した基本バージョンを公開。問題があったら即お蔵入りになります。

基本版ということで特に何か面白いことをやっているわけではなくて、これからのバージョンアップのベースになるものなので、いたって普通・・・かなって感じ。想定している使い方は、RandomTypo.asをスーパークラスにしてカスタマイズする使い方がメインです。以下サンプルとコード。

※テキストはオバマ大統領の演説より引用しました。

This movie requires Flash Player 9.0.0

コード(長いよー

package jp.nulldesign.text
{
import flash.display.Sprite;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.events.Event;
import flash.events.MouseEvent;
import jp.nulldesign.utils.TimerHelper;

public class RandomTypo extends Sprite
{

public static var isDynamic:Boolean = true;
public static var typoList:Array;
private static const CMIN:Number = 33;
private static const CMAX:Number = 126;

private var _text:String;
private var _textList:Array;
private var _dynamicTextList:Array;
private var _count:uint = 0;
private var _delayCount:uint = 0;
private var _speed:uint = 10;

/**
*	basic
*
*
*/
public function RandomTypo():void
{
_textArea.autoSize = TextFieldAutoSize.LEFT;
this.buttonMode = true;
this.mouseChildren = false;
addEventListener( MouseEvent.MOUSE_DOWN, _onMouseDownHandler );
addEventListener( MouseEvent.MOUSE_OVER, _onMouseOverHandler );
addEventListener( MouseEvent.MOUSE_OUT, _onMouseOutHandler );
addEventListener( Event.REMOVED, dispose );
}
public function initialize():void
{
//String.fromCharCode(Math.floor(Math.random()*cMax)+cMin);
if( typoList == null )
{
typoList = [];
for( var i:uint = CMIN; i <= CMAX; i++ )
{
typoList.push( String.fromCharCode( i ) );
}
}
_text = '';
_textList = [];
_dynamicTextList = [];
}

public function render():void
{
if( isDynamic )
{
TimerHelper.addEventListener( this, textRender );
}
}

public function set text( e:String):void
{
_text = e;
_creatTypoList( e );
_textArea.text = e;
_speed = Math.min( 20, Math.floor( 300 / _text.length ) );
}

public function set mouseEnable( e:Boolean ):void
{
this.buttonMode = e;
}

public function set speed( e:uint )	{	_speed = e;	}

/**
*	Mouse Interactions
*
*
*/
private function _onMouseDownHandler( e:MouseEvent ):void
{
onMouseDownHandler();
}

private function _onMouseOverHandler( e:MouseEvent ):void
{
_count = 0;
_delayCount = 0;
render();
onMouseOverHandler();
}

private function _onMouseOutHandler( e:MouseEvent ):void
{
_count = 0;
_delayCount = 0;
//render();
onMouseOutHandler();
}

public function onMouseDownHandler():void{}
public function onMouseOverHandler():void{}
public function onMouseOutHandler():void{}

public function dispose( e:Event = null ):void
{
TimerHelper.removeEventListener( this, textRender );
removeEventListener( MouseEvent.MOUSE_DOWN, _onMouseDownHandler );
removeEventListener( MouseEvent.MOUSE_OVER, _onMouseOverHandler );
removeEventListener( MouseEvent.MOUSE_OUT, _onMouseOutHandler );
removeEventListener( Event.REMOVED, dispose );
}

/**
*	logic
*
*
*/
public function textRender():void
{
if( _delayCount % _speed == 0 )
{
var len:uint = _dynamicTextList.length;
var __text:String = '';
for( var i:uint = 0; i < len; i++ )
{
__text += _dynamicTextList[i][_count];
}
_textArea.text = __text;

_count++;
if( _count >= len )
{
_textArea.text = _text;
_creatTypoList( _text );
TimerHelper.removeEventListener( this, textRender );
}
}
_delayCount++;
}

private function _creatTypoList( e:String ):void
{
_dynamicTextList = [];
var len:uint = e.length;
var LEN:uint = typoList.length;
//テキストの長さ分のリストを確保
for( var i:uint = 0; i < len; i++ )
{
//頭の一文字目から決定していく
_dynamicTextList[i] = [];
for( var j:uint = 0; j < len; j++ )
{
if( i <= j )
{
_dynamicTextList[i][j] = e.charAt(i);
} else {
if( Math.random() < i / len )
{
_dynamicTextList[i][j] = typoList[ Math.floor( Math.random() * LEN ) ];
} else {
_dynamicTextList[i][j] = '-';
}
}
}
}
}
}
}

使い方

※ダイナミックテキストに_textAreaと命名したものをシンボルにしてクラスとひもづけてください。

1. タイマーヘルパーの初期化を行います
TimerHelper.initialize( stage.frameRate );

2. タイマーヘルパーのスタート
TimerHelper.start();

3. ランダムタイポのインスタンスを生成と初期化
var _rt:RandomTypo = new RandomTypo();
_rt.initialize();

4. テキストを入力
_rt.text = "Thank you. God bless you and God bless the United States of America.";

5. 表示します
addChild( _rt );

特にスタートなどは設けていません。これからのバージョンアップで対応。
※昨日公開したTimerhelper.asから少しだけアップデートが入っていますので、ダウンロードファイルにアップデート版を入れておりますのでご安心ください。

RandoomTypo.as

Leave a Reply