forked from: forked from: forked from: ランダムテキストのサンプル

by Takayuki.Yamazaki forked from forked from: forked from: ランダムテキストのサンプル (diff: 5)
等幅にしようぜ

よく見るランダムテキストの練習。
♥0 | Line 96 | Modified 2011-10-05 00:43:10 | MIT License
play

ActionScript3 source code

/**
 * Copyright Takayuki.Yamazaki ( http://wonderfl.net/user/Takayuki.Yamazaki )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/9ti8
 */

// forked from uwi's forked from: forked from: ランダムテキストのサンプル
// forked from takawo's forked from: ランダムテキストのサンプル
// 等幅にしようぜ
// forked from sake's ランダムテキストのサンプル
/*
    よく見るランダムテキストの練習。
*/

package
{
    import flash.display.Sprite;

    [SWF(width="465", height="465", backgroundColor="0xffffff", frameRate="30")]
    public class RandomTextSample extends Sprite
    {


        private var _

        public function RandomTextSample()
        {
            var rand1:RandomText=new RandomText("Wonderfl build flash online.");
            addChild(rand1);
            rand1.x = 20;
            rand1.y = 20;
            var rand2:RandomText=new RandomText("ActionScript3.0 and Papervision3D and Tweener.");
            addChild(rand2);
            rand2.x = 20;
            rand2.y = 40;
            var rand3:RandomText=new RandomText("Progression and Box2DFlashAS3 and Tweensy.");
            addChild(rand3);
            rand3.x = 20;
            rand3.y = 60;
            var rand4:RandomText=new RandomText("Flex and Mete and yui.");
            addChild(rand4);
            rand4.x = 20;
            rand4.y = 80;
            var rand5:RandomText=new RandomText("日本語はあんまりきれいじゃないですやっぱり");
            addChild(rand5);
            rand5.x = 20;
            rand5.y = 100;
        }
    }
}

import flash.display.Sprite;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.utils.Timer;
import flash.events.MouseEvent;

class RandomText extends Sprite
{
    private var MainString:String;
    private var Length:int;
    private var ReplaceCount:int;
    private var ReplaceTimer:Timer;
    private var RandomTimer:Timer;
    public var MainTextField:TextField;

    public function RandomText(main_string:String)
    {
        MainString=main_string;
        Length=MainString.length;
        MainTextField=new TextField();
        MainTextField.defaultTextFormat=new TextFormat("Courier New", 17, 0x000000);
        MainTextField.text=MainString;
        MainTextField.autoSize=TextFieldAutoSize.LEFT;
        MainTextField.addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
        MainTextField.addEventListener(MouseEvent.MOUSE_OUT, onMouseOut);
        addChild(MainTextField);
        ReplaceTimer=new Timer(10);
        ReplaceTimer.addEventListener(TimerEvent.TIMER, onReplaceTimer);
        RandomTimer=new Timer(10);
        RandomTimer.addEventListener(TimerEvent.TIMER, onRandomTimer);
        
    }
    
    private function onMouseOver(e:MouseEvent):void
    {
        if (ReplaceTimer.running) ReplaceTimer.stop();
        RandomTimer.start();
    }
    
    private function onRandomTimer(e:TimerEvent):void
    {
                var text : String = "";
        for(var i:int=0; i < Length; i++) {
                    text += 
                        String.fromCharCode(0x21 + Math.random() * (0x7f - 0x21));
                }
        MainTextField.text = text;
    }

    private function onMouseOut(e:MouseEvent):void
    {
        if(RandomTimer.running) RandomTimer.stop();
        ReplaceCount=0;
        ReplaceTimer.start();
    }

    private function onReplaceTimer(e:TimerEvent):void
    {
                var text : String = "";
                text += MainString.substring(0, ReplaceCount);
        for(var i:int=ReplaceCount; i < Length; i++) {
                    text += 
                        String.fromCharCode(0x21 + Math.random() * (0x7f - 0x21));
                }
        MainTextField.text = text;
        if(ReplaceCount==Length) ReplaceTimer.stop();
        ReplaceCount++;
    }
}