In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

Text transition

An interesting way to make the processing of text, especially in breadcrumb.
Fell free to use it.
Get Adobe Flash player
by euharrison 20 Apr 2011
/**
 * Copyright euharrison ( http://wonderfl.net/user/euharrison )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/7kO2
 */

package
{
    import flash.display.Sprite;
    import flash.events.MouseEvent;
    import flash.events.TimerEvent;
    import flash.text.TextField;
    import flash.text.TextFieldType;
    import flash.text.TextFormat;
    import flash.utils.Timer;
    
    /**
     * @author Harrison (h@harrison.com.br)
     * @since april 21, 2011
     */
    public class TextTransition extends Sprite
    {
        
        private const AVAILABLE:String = 'available';
        private const CLEANING:String = 'cleaning';
        private const GROWING:String = 'growing';
        
        private const transitionFrameRate:uint = 30;
        
        private var viewField:TextField;
        private var inputField:TextField;
        private var finalText:String = '';
        private var commonText:String = '';
        private var state:String = AVAILABLE;
        
        
        public function TextTransition()
        {
            initInterface();
            initTimerCheck();
        }
        
        private function initInterface():void
        {
            var format:TextFormat = new TextFormat('_sans', 14, 0x000000)
            
            viewField = new TextField();
            viewField.defaultTextFormat = format;
            viewField.width = 400;
            viewField.x = 30;
            viewField.y = 50;
            addChild(viewField);
            
            inputField = new TextField();
            inputField.defaultTextFormat = format;
            inputField.width = 400;
            inputField.height = 20;
            inputField.x = 30;
            inputField.y = 100;
            inputField.type = TextFieldType.INPUT;
            inputField.border = true;
            inputField.text = 'type you text here, try use common initial text';
            addChild(inputField);
            
            var button:Sprite = new Sprite();
                button.graphics.lineStyle(1)
                button.graphics.beginFill(0xcccccc);
                button.graphics.drawRect(0, 0, 110, 20);
                button.x = 30;
                button.y = 130;
                button.buttonMode = true;
                button.mouseChildren = false;
                button.addEventListener(MouseEvent.CLICK, doTransition);
           addChild(button);
           
            var buttonLabel:TextField = new TextField();
                buttonLabel.defaultTextFormat = format;
                buttonLabel.selectable = false;
                buttonLabel.x = 10;
                buttonLabel.text = 'do transition';
            button.addChild(buttonLabel);
        }
        
        private function initTimerCheck():void 
        {
            var timer:Timer = new Timer(1000 / transitionFrameRate);
                timer.addEventListener(TimerEvent.TIMER, updateText);
            timer.start();
        }
        
        private function doTransition(e:MouseEvent):void 
        {
            //save our final objetive
            finalText = inputField.text;
            
            //save the letters that doesnt change
            discoveryCommonText();
            
            //make text available to change, if dont
            state = AVAILABLE;
        }
        
        private function discoveryCommonText():void 
        {
            //start commom text as empty
            commonText = '';
            
            //for each letter in final text, compare with actual text of viewField
            for (var i:int = 0; i < finalText.length; i++) 
            {
                if (finalText.charAt(i) == viewField.text.charAt(i))
                {
                    //if commom, add to common text
                    commonText += finalText.charAt(i);
                }
                else
                {
                    //if different, stop everythig
                    return;
                }
            }
        }
        
        private function updateText(e:TimerEvent):void 
        {
            switch (state) 
            {
                case AVAILABLE:
                    if (viewField.text != finalText) 
                    {
                        //something different? go to clean;
                        state = CLEANING;
                    }
                break;
                case CLEANING:
                    if (viewField.text == commonText)
                    {
                        //clean enought, go to growing
                        state = GROWING;
                    }
                    else
                    {
                        //clean one more
                        viewField.replaceText(viewField.length - 1, viewField.length, '');
                    }
                break;
                case GROWING:
                    if (viewField.text == finalText) 
                    {
                        //grow enought, be avilable for next transition
                        state = AVAILABLE;
                    }
                    else
                    {
                        //insert one more letter
                        viewField.appendText(finalText.charAt(viewField.length));
                    }
                break;
            }
        }
        
    }
    
}