This ActionScript 3.0 class is inspired by Mathieu Badimon’s text effect on his Lab site. The class extends TextField and utilizes a randomText property to trigger the randomized text effect.
For example,
var random_tf:RandomTextField = new RandomTextField; random_tf.randomText = "Hello WTFlash!";
There are some optional parameters you can set which allow you to control the number of random characters to display per character of text, the placeholder string, and the delay interval between character swaps.
var random_tf:RandomTextField = new RandomTextField(4, "*", 1000);
or
var random_tf:RandomTextField = new RandomTextField(); random_tf.numberRandomCharacters = 4; random_tf.placeholder = "*"; random_tf.swapDelay = 1000;
Here is the class (NOTE: I’m trying out MonsterDebugger in my workflow):
package com.jasonwoan.text {
/**
* [RandomTextField]
*
* @class RandomTextField
* @author Jason Woan
* @version 1.0.0
* @date Feb 7, 2009
* @langversion ActionScript 3.0
* @playerversion Flash Player 9
*
* @history 1.0.0 (Feb 7, 2009) Created initial version of RandomTextField for AS3.
*/
//--------------------------------------------------
// IMPORT STATEMENTS
//--------------------------------------------------
import nl.demonsters.debugger.MonsterDebugger;
import flash.events.*;
import flash.text.*;
import flash.utils.Timer;
public class RandomTextField extends TextField {
//--------------------------------------------------
// PRIVATE STATIC PROPERTIES
//--------------------------------------------------
/**
* The fully qualifed class name and path. This can be accessed from the class but the value
* can also be retrieved from the instance using the getter fullyQualifedClassPath
*/
private static const FULLY_QUALIFIED_CLASS_PATH: String = "com.jasonwoan.text.RandomTextField";
//--------------------------------------------------
// PRIVATE PROPERTIES
//--------------------------------------------------
/**
* A reference to this instance.
*/
private var _instance:RandomTextField;
/**
* The random text string
*/
private var _random_text:String;
/**
* The number of randomized characters to display per character
*/
private var _num_random:uint = 2;
/**
* A timer used to display the randomized characters
*/
private var _timer:Timer;
/**
* The placeholder character
*/
private var _placeholder:String = "+";
/**
* A string of characters
*/
private var _characters:String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYX01234567890.!@#$%&";
/**
* An array of characters.
*/
private var _char_arr:Array;
/**
* The current index in the message
*/
private var _text_index:uint;
/**
* The current random index
*/
private var _random_index:uint;
/**
* Timer delay
*/
private var _timer_delay:Number = 34;
//--------------------------------------------------
// CONSTRUCTOR
//--------------------------------------------------
/**
* Constructor
*/
public function RandomTextField(p_num_random:uint = 2, p_placeholder:String = "+", p_swap_delay:Number = 34)
{
MonsterDebugger.trace(this, "[" + fullyQualifiedClassPath + "] constructor:");
super();
_instance = this;
_num_random = p_num_random;
_placeholder = p_placeholder;
_timer_delay = p_swap_delay;
init();
}
//--------------------------------------------------
// GETTERS / SETTERS
//--------------------------------------------------
/**
* Returns the fully qualified class path which includes the class package and class name as a string
*
* @return - String representing the full class path.
* For example: com.jasonwoan.text.RandomTextField
*/
public function get fullyQualifiedClassPath(): String
{
return RandomTextField.FULLY_QUALIFIED_CLASS_PATH;
}
/**
* Returns the random text string.
* @return
*/
public function get randomText():String
{
return _instance._random_text;
}
/**
* Sets the random text string
* @param
* @return void
*/
public function set randomText(p_param:String):void
{
MonsterDebugger.trace(this,"[" + fullyQualifiedClassPath + ".randomText]");
_random_text = p_param;
_text_index = 0;
_random_index = 0;
// start the randomization typing
_timer.stop();
_timer.reset();
_timer.start();
}
/**
* Returns the delay between character swaps.
* @return
*/
public function get swapDelay():Number
{
return _timer_delay;
}
/**
* Sets the delay between character swaps.
* @param
* @return void
*/
public function set swapDelay(p_param:Number):void
{
_timer_delay = p_param;
_timer.delay = _timer_delay;
}
/**
* Returns the placeholder character.
* @return
*/
public function get placeholder():String
{
return _placeholder;
}
/**
* Sets the placeholder character.
* @param
* @return void
*/
public function set placeholder(p_param:String):void
{
_placeholder = p_param;
}
/**
* Returns the number of random characters to swap before the correct
* character is displayed.
* @return
*/
public function get numberRandomCharacters():uint
{
return _num_random;
}
/**
* Sets the number of random characters to swap before the correct
* character is displayed.
* @param
* @return void
*/
public function set numberRandomCharacters(p_param:uint):void
{
_num_random = p_param;
}
//--------------------------------------------------
// EVENT HANDLERS
//--------------------------------------------------
/**
* Invoked when the timer event is triggered.
* @param p_event ()
* @return void
*/
private function onTimer(p_event:TimerEvent):void
{
cycleCharacter();
}
//--------------------------------------------------
// PUBLIC METHODS
//--------------------------------------------------
/**
* Destroys this object for garbage collection.
*
* @return void
*/
public function destroy():void
{
MonsterDebugger.trace(this,"[" + fullyQualifiedClassPath + ".destroy]");
_instance = null;
}
//--------------------------------------------------
// PRIVATE METHODS
//--------------------------------------------------
/**
* Initializes class instance.
*
* @return void
*/
private function init():void
{
MonsterDebugger.trace(this,"[" + fullyQualifiedClassPath + ".init]");
_char_arr = _characters.split("");
_timer = new Timer(_timer_delay);
_timer.addEventListener(TimerEvent.TIMER, onTimer);
}
/**
* Cycles through each character of the alphabet to find a matching character.
* @return void
*/
private function cycleCharacter():void
{
// check to see if the message index is less than the message length
if (_text_index == _random_text.length) {
_timer.stop();
return;
}
var s:String = "";
var random_char:String;
// check for next
if (_random_index == _num_random) {
// set correct character
random_char = _random_text.charAt(_text_index);
// move on to next character
_random_index = 0;
_text_index++;
} else {
random_char = _char_arr[Math.floor(Math.random() * _char_arr.length)];
_random_index++;
}
// build string
for (var i:uint = 0; i < _random_text.length; i++) {
if (i == _text_index) {
// add random character;
s += random_char;
} else
if (i > _text_index) {
// add placeholder;
s += _placeholder;
} else
if (i < _text_index) {
// add correct character
s += _random_text.charAt(i);
}
}
_instance.text = s;
}
}
}
Enjoy!
