JAVAC.JP
HOSHI TETSUYA 星鉄矢
2005/07/26
hossy@javac.jp
http://www.javac.jp
Google
WWW を検索 WWW.JAVAC.JP を検索

MTASC | ActionScript 2とFlash OOPのすすめ


メニュー

トップへ

インストール マニュアル

Hello Worldとバッチファイル
画像を扱う様々な方法
実際のアプリケーション:時計
実際のアプリケーション:3Dビューア

trace文がない - デバッガの必要性

手軽にMTASC - サーバサイドコンパイラ
実際のアプリケーション:時計

ここでは、以下の時計を作成してみる。Now Loading処理なども含んでおり、これまでのものよりは少々実例と言える。



ソースは以下のようになる。

Clock.as
---
class Clock {
    private var BACK_MCNAME :String = "back";
    private var BASE_MCNAME :String = "base";
    private var RUNNABLE_MCNAME :String = "runnable";
    private var BACK_DEPTH :Number = 1;
    private var BASE_DEPTH :Number = 10;
    private var RUNNABLE_DEPTH :Number = 100;
    private var BACK_FILENAME :String = "img/tocho.swf";
    private var NOWLOADING_FILENAME :String = "img/nowloading.swf";
    private var BASE_CENTER_X :Number = 40;
    private var BASE_CENTER_Y :Number = 40;
    private var base :MovieClip;
    private var LINE_MCNAME1 :String = "line1";
    private var LINE_MCNAME2 :String = "line2";
    private var LINE_MCNAME3 :String = "line3";
    private var LINE_DEPTH1 :Number = 1;
    private var LINE_DEPTH2 :Number = 2;
    private var LINE_DEPTH3 :Number = 3;
    private var LINE_LENGTH1 :Number = 15;
    private var LINE_LENGTH2 :Number = 25;
    private var LINE_LENGTH3 :Number = 25;
    private var LINE_STROKE1 :Number = 3;
    private var LINE_STROKE2 :Number = 3;
    private var LINE_STROKE3 :Number = 1;

    function Clock (path) {
        var ref = this;
        path.createEmptyMovieClip(BACK_MCNAME, BACK_DEPTH);
        path.createEmptyMovieClip(BASE_MCNAME, BASE_DEPTH);
        path.createEmptyMovieClip(RUNNABLE_MCNAME, RUNNABLE_DEPTH);
        var back :MovieClip = path[BACK_MCNAME];
        base = path[BASE_MCNAME];
        var runnable :MovieClip = path[RUNNABLE_MCNAME];
        back.createEmptyMovieClip("seed", 1);
        back.seed.loadMovie(BACK_FILENAME);
        back.createEmptyMovieClip("nowloading", 10);
        back.nowloading.loadMovie(NOWLOADING_FILENAME);
        back.nowloading._x = 240;
        back.nowloading._y = 220;
        base._x = BASE_CENTER_X;
        base._y = BASE_CENTER_Y;
        runnable.onEnterFrame = function () :Void {
            ref.onEnterFrameHandler();
        };
        base._visible = false;
        back.onEnterFrame = function () :Void {
            var obj :Object = new Object();
            obj.target = this;
            ref.backLoadHandler(obj);
        };
    }

    public function onEnterFrameHandler () :Void {
        var now :Date = new Date();
        var hh :Number = now.getHours();
        var mm :Number = now.getMinutes();
        var ss :Number = now.getSeconds();
        var theta1 :Number = 2 * Math.PI * (hh + mm / 60) / 12 - Math.PI / 2;
        var theta2 :Number = 2 * Math.PI * (mm + ss / 60) / 60 - Math.PI / 2;
        var theta3 :Number = 2 * Math.PI * ss / 60 - Math.PI / 2;
        base.createEmptyMovieClip(LINE_MCNAME1, LINE_DEPTH1);
        base.createEmptyMovieClip(LINE_MCNAME2, LINE_DEPTH2);
        base.createEmptyMovieClip(LINE_MCNAME3, LINE_DEPTH3);
        var mc1 :MovieClip = base[LINE_MCNAME1];
        var mc2 :MovieClip = base[LINE_MCNAME2];
        var mc3 :MovieClip = base[LINE_MCNAME3];
        mc1.lineStyle(LINE_STROKE1, 0x000000);
        mc1.moveTo(0, 0);
        mc1.lineTo(LINE_LENGTH1 * Math.cos(theta1), LINE_LENGTH1 * Math.sin(theta1))
        mc2.lineStyle(LINE_STROKE2, 0x000000);
        mc2.moveTo(0, 0);
        mc2.lineTo(LINE_LENGTH2 * Math.cos(theta2), LINE_LENGTH2 * Math.sin(theta2))
        mc3.lineStyle(LINE_STROKE3, 0x000000);
        mc3.moveTo(0, 0);
        mc3.lineTo(LINE_LENGTH3 * Math.cos(theta3), LINE_LENGTH3 * Math.sin(theta3))
    }

    public function backLoadHandler (event) :Void {
        var mc :MovieClip = event.target;
        var total :Number = mc.seed.getBytesTotal();
        var loaded :Number = mc.seed.getBytesLoaded();
        if (total == loaded && total > 2) {
            var obj :Object = new Object();
            obj.target = mc;
            backLoadedHandler(obj);
        }
    }

    public function backLoadedHandler (event) :Void {
        var mc :MovieClip = event.target;
        mc.nowloading.removeMovieClip();
        base._visible = true;
    }

    static function main () :Void {
        var clock :Clock = new Clock(_root);
    }
}

参考ファイルダウンロード
clock.zip





All Contents Copyright (C) 2005 HOSHI Tetsuya
Home