ランダムテキストのサンプル。
以前作ったものを、研究室勉強会用に書き直し。
こんな感じになります。
以下、ソースです(swfを右クリックしても見れます)。
ランダムテキストクラス。
package
{
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.events.TimerEvent;
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.text.TextFormat;
import flash.utils.Timer;
public class RandomText extends Sprite
{
[Embed(source='./asset/Fine Bit.ttf',embedAsCFF="false",fontName='myFont',mimeType='application/x-font')]
private var myFont:Class;
private static const STRINGS:String = "_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890";
private static const STRINGS_LENGTH:int = STRINGS.length - 1;
private var _text:String;
private var _length:int;
private var _textFiled:TextField;
private var _randomTimer:Timer;
private var _replaceTimer:Timer;
private var _count:int;
public function RandomText(text:String, fontSize:int)
{
_text = text;
_length = _text.length;
_textFiled = addChild(new TextField) as TextField;
_textFiled.embedFonts = true;
_textFiled.selectable = false;
_textFiled.mouseEnabled = false;
_textFiled.defaultTextFormat = new TextFormat("myFont", fontSize, 0x000000);
_textFiled.text = _text;
_textFiled.autoSize = TextFieldAutoSize.LEFT;
buttonMode = true;
useHandCursor = true;
addEventListener(MouseEvent.MOUSE_OVER, onMouseOver);
_randomTimer = new Timer(20, 20);
_randomTimer.addEventListener(TimerEvent.TIMER, onRandomTimer);
_randomTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onRandomTimerComplete);
_replaceTimer = new Timer(20);
_replaceTimer.addEventListener(TimerEvent.TIMER, onReplaceTimer);
}
private function onMouseOver(e:MouseEvent=null):void
{
if(_replaceTimer.running) _replaceTimer.stop();
_randomTimer.reset();
_randomTimer.start();
}
private function onRandomTimer(e:TimerEvent):void
{
var str:String = "";
for(var i:int=0 ; i<_length ; i++) str += STRINGS.charAt((Math.random() * STRINGS_LENGTH)>>0);
_textFiled.text = str;
}
private function onRandomTimerComplete(e:TimerEvent):void
{
_count = 0;
_replaceTimer.start();
}
private function onReplaceTimer(e:TimerEvent):void
{
var str:String = "";
var i:int;
var t:int = _length - _count;
for(i = 0 ; i < _count; i++) str += _text.charAt(i);
for(i = 0 ; i < t ; i++) str += STRINGS.charAt((Math.random() * STRINGS_LENGTH)>>0);
_textFiled.text = str;
if(_count == _length) _replaceTimer.stop();
_count ++;
}
public function set textColor(value:uint):void
{
_textFiled.textColor = value;
}
public function get textColor():uint
{
return _textFiled.textColor;
}
}
}
メインクラス。
package
{
import com.adobe.viewsource.ViewSource;
import flash.display.Graphics;
import flash.display.Shape;
import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageQuality;
import flash.display.StageScaleMode;
import flash.events.MouseEvent;
import org.libspark.betweenas3.BetweenAS3;
import org.libspark.betweenas3.easing.Quintic;
public class Main extends Sprite
{
// ランダムテキストとバーを用意
private var text1:RandomText, text2:RandomText, text3:RandomText;
private var bar1:Shape, bar2:Shape, bar3:Shape;
// クリックされたものを保持する一時的な変数
private var clicked:RandomText;
public function Main()
{
// ステージの設定
stage.scaleMode=StageScaleMode.NO_SCALE;
stage.align=StageAlign.TOP_LEFT;
stage.quality=StageQuality.HIGH;
stage.frameRate=40;
// 「右クリックでソース表示」が出来るように。
ViewSource.addMenuItem(this, "srcview/index.html");
// テキストを作成
bar1=createBar(50, 122);
bar2=createBar(50, 172);
bar3=createBar(50, 222);
// バーを作成
text1=createText("adobe_flash_builder.", 50, 120);
text2=createText("adobe_photoshop.", 50, 170);
text3=createText("adobe_illustrator.", 50, 220);
}
// ボタンがクリックされたら実行する関数
private function onClick(e:MouseEvent):void
{
if (clicked == e.target) return ;
// ひとつ前にクリックされていたテキストを元に戻す。
var bar:Shape;
if (clicked)
{
clicked.textColor=0x000000;
switch(clicked)
{
case text1:
bar=bar1;
break;
case text2:
bar=bar2;
break;
case text3:
bar=bar3;
break;
}
bar.scaleX=0.2;
BetweenAS3.tween(bar, {scaleX:0}, null, 0.2, Quintic.easeOut).play();
}
// クリックされたテキストのバーを伸ばす
clicked=e.target as RandomText;
clicked.textColor=0xffffff;
switch(clicked)
{
case text1:
bar=bar1;
break;
case text2:
bar=bar2;
break;
case text3:
bar=bar3;
break;
}
bar.scaleX=0.7;
BetweenAS3.tween(bar, {scaleX:1}, null, 0.2, Quintic.easeOut).play();
}
// バーを作成する関数
private function createBar(x:Number, y:Number):Shape
{
var shape:Shape=addChild(new Shape)as Shape;
var g:Graphics=shape.graphics;
g.beginFill(0x770000, 1);
g.drawRect(0, 0, 160, 16);
shape.scaleX=0;
shape.x=x;
shape.y=y;
return shape;
}
// テキストを作成する関数
private function createText(text:String, x:Number, y:Number):RandomText
{
var t:RandomText=addChild(new RandomText(text, 25))as RandomText;
t.x=x;
t.y=y;
t.addEventListener(MouseEvent.CLICK, onClick);
return t;
}
}
}
