RandomText
ランダムなストリングを表示して、左から順に確定していく定番トランジション。置き換える文字を文字コードで指定できるようにして、各イベントも送出してみた。
このサンプルだと、上から順に
- 半角英数字 + 半角記号
- 半角英字(大文字)
- 半角英字(小文字)
- 半角数字
- 半角カナ
な感じでランダムになるはず。
以下、ソース
- ActionScript
- RandomText.as
- Source
package {
import flash.events.Event;
import flash.events.EventDispatcher;
import flash.events.ProgressEvent;
import flash.text.TextField;
public class RandomText extends EventDispatcher {
private var _textField:TextField;
private var _fixFrame:int;
private var _minCharCode:uint;
private var _maxCharCode:uint;
private var _text:String;
private var _counter:int;
public function get textField():TextField { return _textField; }
public function set textField(value:TextField):void {
_textField = value;
_text = _textField.text;
}
public function get fixFrame():int { return _fixFrame; }
public function set fixFrame(value:int):void {
_fixFrame = value;
}
public function get minCharCode():uint { return _minCharCode; }
public function set minCharCode(value:uint):void {
_minCharCode = value;
}
public function get maxCharCode():uint { return _maxCharCode; }
public function set maxCharCode(value:uint):void {
_maxCharCode = value;
}
public function RandomText(textField:TextField, fixFrame:int, minCharCode:uint = 33, maxCharCode:uint = 126) {
_textField = textField;
_fixFrame = fixFrame;
_minCharCode = minCharCode;
_maxCharCode = maxCharCode;
_text = textField.text;
textField.text = '';
}
public function start():void {
_counter = 0;
textField.addEventListener(Event.ENTER_FRAME, onEnterFrame);
dispatchEvent(new Event(Event.OPEN, false, false));
}
private function onEnterFrame(e:Event):void {
var textField:TextField = e.currentTarget as TextField;
var pointer:int = Math.floor(_counter / _fixFrame);
var text:String = '';
var i:int;
for (i = 0; i < _text.length; i ++) {
text += (i < pointer)? _text.charAt(i): getRandomString();
}
textField.text = text;
if(!(_counter % _fixFrame)) dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, false, false, pointer, _text.length));
if (pointer == _text.length) {
textField.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
dispatchEvent(new Event(Event.COMPLETE, false, false));
}
_counter ++;
}
private function getRandomString():String {
var randomCharCode:int = _minCharCode + Math.random() * (_maxCharCode - _minCharCode);
return String.fromCharCode(randomCharCode);
}
}
}- package {
- import flash.events.Event;
- import flash.events.EventDispatcher;
- import flash.events.ProgressEvent;
- import flash.text.TextField;
- public class RandomText extends EventDispatcher {
- private var _textField:TextField;
- private var _fixFrame:int;
- private var _minCharCode:uint;
- private var _maxCharCode:uint;
- private var _text:String;
- private var _counter:int;
- public function get textField():TextField { return _textField; }
- public function set textField(value:TextField):void {
- _textField = value;
- _text = _textField.text;
- }
- public function get fixFrame():int { return _fixFrame; }
- public function set fixFrame(value:int):void {
- _fixFrame = value;
- }
- public function get minCharCode():uint { return _minCharCode; }
- public function set minCharCode(value:uint):void {
- _minCharCode = value;
- }
- public function get maxCharCode():uint { return _maxCharCode; }
- public function set maxCharCode(value:uint):void {
- _maxCharCode = value;
- }
- public function RandomText(textField:TextField, fixFrame:int, minCharCode:uint = 33, maxCharCode:uint = 126) {
- _textField = textField;
- _fixFrame = fixFrame;
- _minCharCode = minCharCode;
- _maxCharCode = maxCharCode;
- _text = textField.text;
- textField.text = '';
- }
- public function start():void {
- _counter = 0;
- textField.addEventListener(Event.ENTER_FRAME, onEnterFrame);
- dispatchEvent(new Event(Event.OPEN, false, false));
- }
- private function onEnterFrame(e:Event):void {
- var textField:TextField = e.currentTarget as TextField;
- var pointer:int = Math.floor(_counter / _fixFrame);
- var text:String = '';
- var i:int;
- for (i = 0; i < _text.length; i ++) {
- text += (i < pointer)? _text.charAt(i): getRandomString();
- }
- textField.text = text;
- if(!(_counter % _fixFrame)) dispatchEvent(new ProgressEvent(ProgressEvent.PROGRESS, false, false, pointer, _text.length));
- if (pointer == _text.length) {
- textField.removeEventListener(Event.ENTER_FRAME, onEnterFrame);
- dispatchEvent(new Event(Event.COMPLETE, false, false));
- }
- _counter ++;
- }
- private function getRandomString():String {
- var randomCharCode:int = _minCharCode + Math.random() * (_maxCharCode - _minCharCode);
- return String.fromCharCode(randomCharCode);
- }
- }
- }
実行してるサンプル。Completeイベント取得して、次のトランジションに移るとかする・・・ことあるのかなぁ。やりっぱなしなシチュエーションの方が多そう、実際問題。
- ActionScript
- TestClass_RandomText.as
- Source
package {
import flash.events.Event;
import flash.events.ProgressEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
public class TestClass_RandomText extends Sprite {
private var _counter:int;
public function TestClass_RandomText() {
_counter = 0;
var strings:Array = [
'Flabo ActionScript/JavaScriptでRIA!!RIA!!',
'getColorBoundsRect()のバグ?',
'CSS+抜きのgifでアイコンカラー切替',
'配列のエレメントの重複を削除する',
'カスタムイベントのtypeに’allComplete’は使えない'
];
var charCodeRange:Array = [
{minCharCode: parseInt('0021', 16), maxCharCode: parseInt('007D', 16)}, // 半角英数字 + 半角記号
{minCharCode: parseInt('0041', 16), maxCharCode: parseInt('005A', 16)}, // 半角英字(大文字)
{minCharCode: parseInt('0061', 16), maxCharCode: parseInt('007A', 16)}, // 半角英字(小文字)
{minCharCode: parseInt('0030', 16), maxCharCode: parseInt('0039', 16)}, // 半角数字
{minCharCode: parseInt('FF66', 16), maxCharCode: parseInt('FF9D', 16)} // 半角カナ
];
var i:int, textField:TextField, randomText:RandomText;
for (i = 0; i < strings.length; i ++) {
textField = createTextField(strings[i]);
addChild(textField);
randomText = new RandomText(textField, 5, charCodeRange[i].minCharCode, charCodeRange[i].maxCharCode);
randomText.addEventListener(Event.OPEN, onOpen);
randomText.addEventListener(ProgressEvent.PROGRESS, onProgress);
randomText.addEventListener(Event.COMPLETE, onComplete);
randomText.start();
}
}
private function createTextField(str:String):TextField {
_counter ++;
var textFormat:TextFormat = new TextFormat();
textFormat.font = '_等幅';
textFormat.size = 11;
textFormat.color = 0x000000;
var textField:TextField = new TextField();
textField.autoSize = TextFieldAutoSize.LEFT;
textField.defaultTextFormat = textFormat;
textField.text = str;
textField.x = 10;
textField.y = textField.height * _counter;
return textField;
}
private function onOpen(e:Event):void {
trace(e);
}
private function onProgress(e:ProgressEvent):void {
trace(e);
}
private function onComplete(e:Event):void {
trace(e);
}
}
}- package {
- import flash.events.Event;
- import flash.events.ProgressEvent;
- import flash.text.TextField;
- import flash.text.TextFieldAutoSize;
- import flash.text.TextFormat;
- public class TestClass_RandomText extends Sprite {
- private var _counter:int;
- public function TestClass_RandomText() {
- _counter = 0;
- var strings:Array = [
- 'Flabo ActionScript/JavaScriptでRIA!!RIA!!',
- 'getColorBoundsRect()のバグ?',
- 'CSS+抜きのgifでアイコンカラー切替',
- '配列のエレメントの重複を削除する',
- 'カスタムイベントのtypeに’allComplete’は使えない'
- ];
- var charCodeRange:Array = [
- {minCharCode: parseInt('0021', 16), maxCharCode: parseInt('007D', 16)}, // 半角英数字 + 半角記号
- {minCharCode: parseInt('0041', 16), maxCharCode: parseInt('005A', 16)}, // 半角英字(大文字)
- {minCharCode: parseInt('0061', 16), maxCharCode: parseInt('007A', 16)}, // 半角英字(小文字)
- {minCharCode: parseInt('0030', 16), maxCharCode: parseInt('0039', 16)}, // 半角数字
- {minCharCode: parseInt('FF66', 16), maxCharCode: parseInt('FF9D', 16)} // 半角カナ
- ];
- var i:int, textField:TextField, randomText:RandomText;
- for (i = 0; i < strings.length; i ++) {
- textField = createTextField(strings[i]);
- addChild(textField);
- randomText = new RandomText(textField, 5, charCodeRange[i].minCharCode, charCodeRange[i].maxCharCode);
- randomText.addEventListener(Event.OPEN, onOpen);
- randomText.addEventListener(ProgressEvent.PROGRESS, onProgress);
- randomText.addEventListener(Event.COMPLETE, onComplete);
- randomText.start();
- }
- }
- private function createTextField(str:String):TextField {
- _counter ++;
- var textFormat:TextFormat = new TextFormat();
- textFormat.font = '_等幅';
- textFormat.size = 11;
- textFormat.color = 0x000000;
- var textField:TextField = new TextField();
- textField.autoSize = TextFieldAutoSize.LEFT;
- textField.defaultTextFormat = textFormat;
- textField.text = str;
- textField.x = 10;
- textField.y = textField.height * _counter;
- return textField;
- }
- private function onOpen(e:Event):void {
- trace(e);
- }
- private function onProgress(e:ProgressEvent):void {
- trace(e);
- }
- private function onComplete(e:Event):void {
- trace(e);
- }
- }
- }
About this entry
You’re currently reading “RandomText,” an entry on jp.ferv.blog
- Published:
- Wed, Apr 8th, 2009 at 2:12 PM
- Author:
- dsk
- Category:
- Web
- Tags:
- ActionScript 3.0
No comments
Jump to comment form | comments rss | trackback uri