▼ActionScript 2.0メモ▼
日付の表示


日付の表示を行うFlashを作成する。



ソースコード
DateEx.as
//日付の表示
class DateEx extends MovieClip {
    private var WEEK:Array=//曜日
        ["日", "月", "火", "水", "木", "金", "土"];
    private var label:TextField;//ラベル

    //コンストラクタ
    function DateEx() {
        //ラベルの追加
        label=Util.addLabel(this);
        label.setFontColor(0xFF0000);

        //定期イベントの開始
        setInterval(this,"onTick",100);
    }

    //タイマーイベントの処理
    private function onTick():Void {
        var date:Date=new Date();//日付
        label.setText(
            date.getFullYear()        +"/"+//年
            num2str(date.getMonth()+1)+"/"+//月
            num2str(date.getDate())   +" "+//日
            "("+WEEK[date.getDay()]+") "+  //曜日
            num2str(date.getHours())  +":"+//時
            num2str(date.getMinutes())+":"+//分
            num2str(date.getSeconds()));   //秒
    }

    //数値補正
    private function num2str(num:Number):String {
        if (num<10) return "0"+num;
        return ""+num;
    }
}

Util.as
//ユーティリティ
class Util {
    //ラベルの追加
    static function addLabel(parent:MovieClip,
        text:String):TextField {
        //初期値
        if (text==undefined) text="";

        //インスタンス名の自動生成
        if (parent.nameIdx==undefined) parent.nameIdx=0;
        var name:String="_name"+(parent.nameIdx++);

        //テキストフィールドの追加
        parent.createTextField(name,
            parent.getNextHighestDepth(),0,0,0,0);
        var mc:TextField=parent[name];
        mc.autoSize  ="left";//オートサイズ
        mc.selectable=false; //選択不可
        mc.text=text;        //テキスト
        mc._x  =0;           //X座標
        mc._y  =0;           //Y座標
        
        //書式
        mc.format=new TextFormat();
        mc.format.font ="_等幅"; //フォント
        mc.format.size =12;      //文字サイズ
        mc.format.color=0x000000;//文字色
        mc.setTextFormat(mc.format);
        
        //XY座標の指定
        mc.setXY=function(_x:Number,_y:Number) {
            this._x=_x;
            this._y=_y;
        }

        //テキストの指定
        mc.setText=function(_text:String) {
            this.text=_text;
            this.setTextFormat(this.format);
        }
        
        //文字サイズの指定
        mc.setFontSize=function(size:Number) {
            this.format.size=size; 
            this.setTextFormat(this.format);
        }
        
        //文字色の指定
        mc.setFontColor=function(color:Number) {
            this.format.color=color;
            this.setTextFormat(this.format);
        }
        return mc;
    }
}

application.xml
<?xml version="1.0" encoding="iso-8859-1"?>
<movie version="7" width="240" height="240" framerate="30">
    <background color="#ffffff"/>
    <clip import="classes.swf" />  
    <frame>
        <library>
            <!-- クラス -->
            <clip id="Application" class="DateEx" />

            <!-- リソース -->
        </library>
        <place id="Application" name="app" x="0" y="0" depth="1000" />
    </frame>
</movie>


コンパイル
mtasc -version 7 -swf classes.swf -header 240:240:30 DateEx
swfmill simple application.xml DateEx.swf



−戻る−