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

prog.hu: String effekt megvalósítása. Hogyan? 1.

http://prog.hu/tudastar/134282/String+effekt+megvalositasa+Hogyan.html

modified: 2011-05-08 17:15 (GMT +01:00) - add hitBox to detect the "correct" hit area (in the original version I used the default hitArea (for the MenuItems I draw an invisible rectangle which covers the original text), and some cases the ROLL_OVER/ROLL_OUT events generated periodically, if the mouse hits the generated random text, but outside the hidden hitArea (because the generated text wider than) and the next step the generated text is narrowed than the hit area and so on...)
Get Adobe Flash player
by szbzs2004 08 May 2011
/**
 * Copyright szbzs2004 ( http://wonderfl.net/user/szbzs2004 )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/gyuP
 */

package  {

    import flash.display.MovieClip;
    
    public class SEMH extends MovieClip {

        private var menuItems:Array = ["Home", "Contact", "1", "Ez meg egy jó hosszú"];

        public function SEMH() {
            var itemsY:Number = 0;
            for (var i:int = 0; i < menuItems.length; ++i) {
                  var m:MenuItem = new MenuItem(menuItems[i]);
                  m.y = itemsY;
                  itemsY += m.height;
                  addChild(m);
            }
        }
    }
}

import flash.display.Graphics;
import flash.display.Sprite;
import flash.events.MouseEvent;

class MenuItem extends Sprite {

    private static const TEXT_COLOR_OUT:uint = 0x808080;
    private static const TEXT_COLOR_OVER:uint = 0xff0000;
    
    private var menuTextField:AnimatedTextField;
    
    public function MenuItem(s:String) {
        // menüszöveg létrehozása
        menuTextField = new AnimatedTextField(s);
        menuTextField.textColor = TEXT_COLOR_OUT;
        addChild(menuTextField);
        // hitArea rajzolása
        var hitBox = new Sprite();
        hitBox.graphics.beginFill(0xff0000, 0);
        hitBox.graphics.drawRect(0, 0, width, height);
        hitBox.graphics.endFill();
        addChildAt(hitBox, 0);
        hitArea = hitBox;
        // buttonMode bekapcsolása
        buttonMode = true;
        // eseménykezelők
        addEventListener(MouseEvent.ROLL_OVER, itemOver);
        addEventListener(MouseEvent.ROLL_OUT, itemOut);
    }
    
    private function itemOver(e:MouseEvent):void {
        menuTextField.textColor = TEXT_COLOR_OVER;
        menuTextField.doAnimation();
        e.updateAfterEvent();
    }

    private function itemOut(e:MouseEvent):void {
        menuTextField.textColor = TEXT_COLOR_OUT;
        menuTextField.doAnimation();
        e.updateAfterEvent();
    }
}

import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.events.Event;

class AnimatedTextField extends TextField {

    private var baseText:String;
    private var animatedLength:int;
    
    public function AnimatedTextField(s:String) {
        baseText = s;
        autoSize = TextFieldAutoSize.LEFT;
        selectable = false;
        scaleX = scaleY = 3;
        text = baseText;
        mouseEnabled = false;
        animatedLength = 0;
    }
    
    public function doAnimation():void {
        if (baseText.length != 0) {
            if (animatedLength == 0) {
                addEventListener(Event.ENTER_FRAME, animation);
            }
            animatedLength = baseText.length;
            text = randomText(animatedLength);
        }
    }
    
    private function animation(e:Event):void {
        --animatedLength;
        if (animatedLength == 0) {
            removeEventListener(Event.ENTER_FRAME, animation);
            text = baseText;
        } else {
            text = baseText.substr(0, baseText.length - animatedLength) + randomText(animatedLength);
        }
    }
    
    private function randomText(n:int):String {
        var s:String = "";
        while (n-- > 0) {
            s += String.fromCharCode(32 + 96 * Math.random());
        }
        return (s);
    }
}