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 - サーバサイドコンパイラ
実際のアプリケーション:3Dビューア

今までの例は「MTASCではこの程度のアプリしか開発できないのか」との印象も抱きかねない小さなものばかりでしたので、少々複雑な例を掲載します。
MTASCでコンパイルする、完全にFlash OOPに根ざした、3Dビューアアプリケーションです。
画面内をドラッグすることでオブジェクトを3D回転できます。



ソースは以下のようになります。

StructureControl.as
---
class StructureControl{
    private var path :MovieClip;
    private var IOService :XMLIOService;
    private var IOService1 :XMLIOService;
    private var IOService2 :XMLIOService;
    private var IOService3 :XMLIOService;
    private var dataStorage :ModelDataStorage;
    private var paramControl :ParamControl;
    private var drawingControl :DrawingControl;
    private var depthManager :DepthManager;
    private var oldX :Number;
    private var oldY :Number;
    private var VIEW_MC_DEPTH :Number = 100;
    private var MOUSE_MC_DEPTH :Number = 200;
    private var CENTER_X :Number = 200;
    private var CENTER_Y :Number = 200;

    function StructureControl (path :MovieClip) {
        this.path = path;
        IOService = new XMLIOService();
        IOService1 = new XMLIOService();
        IOService2 = new XMLIOService();
        IOService3 = new XMLIOService();
        dataStorage = new ModelDataStorage();
        paramControl = new ParamControl();
        depthManager = new DepthManager();
        path.createEmptyMovieClip("view_mc", VIEW_MC_DEPTH);
        path.view_mc._x = CENTER_X;
        path.view_mc._y = CENTER_Y;
        drawingControl = new DrawingControl(path.view_mc);
        configLoad("config.xml");
    }

    public function configLoad (address :String) :Void {
        removeMouseListener();
        dataStorage.reset();
        IOService.reset();
        IOService.addLoadComplete("configLoaded", this);
        IOService.load(address, null, false);
    }

    public function configLoaded(ex :XML) :Void {
        var nodes :Array = ex.firstChild.childNodes;
        for(var i :Number = 0; i < nodes.length; i++) {
            var n :XMLNode = XMLNode(nodes[i]);
            if (n.nodeName == "model") {
                var defaultScale :Number = Number(n.attributes.scale);
                var defaultLength :Number = Number(n.attributes.length);
                paramControl.setDefaultScale(defaultScale);
                paramControl.setDefaultLength(defaultLength);

                var scale :Number = Number(n.attributes.scale);
                var cx :Number = Number(n.attributes.cx);
                var cy :Number = Number(n.attributes.cy);
                var cz :Number = Number(n.attributes.cz);
                var length :Number = Number(n.attributes.length);
                if (scale > 0) {
                    paramControl.setScale(scale);
                } else {
                    paramControl.resetScale();
                }
                if (length > 0) {
                    paramControl.setScreenLength(length);
                } else {
                    paramControl.resetScreenLength();
                }
                paramControl.setCenterPosition(cx, cy, cz);
            } else if (n.nodeName == "point") {
                var dirname :String = String(n.attributes.dirname);
                var filename :String = String(n.attributes.filename);
                var address :String = dirname + filename;
                dataStorage.setPointAddress(address);
            } else if (n.nodeName == "line") {
                var dirname :String = String(n.attributes.dirname);
                var filename :String = String(n.attributes.filename);
                var address :String = dirname + filename;
                dataStorage.setLineAddress(address);
            } else if (n.nodeName == "mesh") {
                var dirname :String = String(n.attributes.dirname);
                var filename :String = String(n.attributes.filename);
                var address :String = dirname + filename;
                dataStorage.setMeshAddress(address);
            }else{
            }
        }
        var address :String;
        address = dataStorage.getPointAddress();
        if (address.length > 0) {
            pointLoad(address);
        }
        address = dataStorage.getLineAddress();
        if (address.length > 0) {
            lineLoad(address);
        }
        address = dataStorage.getMeshAddress();
        if (address.length > 0) {
            meshLoad(address);
        }
    }

    public function pointLoad (address :String) :Void {
        IOService1.reset();
        IOService1.addLoadComplete("loaded", this);
        IOService1.load(address, null, false);
    }

    public function lineLoad (address :String) :Void {
        IOService2.reset();
        IOService2.addLoadComplete("loaded", this);
        IOService2.load(address, null, false);
    }

    public function meshLoad (address :String) :Void {
        IOService3.reset();
        IOService3.addLoadComplete("loaded", this);
        IOService3.load(address, null, false);
    }

    public function loaded (ex :XML) :Void {
        var nodes :Array = ex.firstChild.childNodes;
        var pointList :Array;
        var lineList :Array;
        var meshList :Array;
        for(var i :Number = 0; i < nodes.length; i++){
            var n: XMLNode = XMLNode(nodes[i]);
            if (n.nodeName == "pointlist") {
                pointList = n.childNodes;
            } else if (n.nodeName == "linelist") {
                lineList = n.childNodes;
            } else if (n.nodeName == "meshlist") {
                meshList = n.childNodes;
            } else {
            }
        }
        if (pointList.length > 0) {
            for (var i :Number = 0; i < pointList.length; i++) {
                var n :XMLNode = XMLNode(pointList[i]);
                if (n.nodeName == "point") {
                    var id :String = String(n.attributes.id);
                    var x :Number = Number(n.attributes.x);
                    var y :Number = Number(n.attributes.y);
                    var z :Number = Number(n.attributes.z);
                    dataStorage.addPoint(id,x,y,z);
                } else {
                }
            }
            dataStorage.centering();
        }
        if (lineList.length > 0) {
            for (var i :Number = 0; i < lineList.length; i++) {
                var n :XMLNode = XMLNode(lineList[i]);
                if (n.nodeName == "line") {
                    var id :String = String(n.attributes.id);
                    var point1 :String = String(n.attributes.point1);
                    var point2 :String = String(n.attributes.point2);
                    dataStorage.addLine(id, point1, point2);
                } else {
                }
            }
            dataStorage.addPointIndexToLineLists();
        }
        if (meshList.length > 0) {
            for(var i :Number = 0; i < meshList.length; i++) {
                var n :XMLNode = XMLNode(meshList[i]);
                if (n.nodeName == "mesh") {
                    var id :String = String(n.attributes.id);
                    var point1 :String = String(n.attributes.point1);
                    var point2 :String = String(n.attributes.point2);
                    var point3 :String = String(n.attributes.point3);
                    dataStorage.addMesh(id, point1, point2, point3);
                } else {
                }
            }
            dataStorage.addPointIndexToMeshLists();
        }
        resetMesh();
        drawMesh();
        addMouseListener();
    }

    public function resetLine () :Void {
        drawingControl.removeAllLines();
    }

    public function resetMesh () :Void {
        drawingControl.removeAllTriangles();
    }

    public function drawLine () :Void {
        drawLines();
    }

    public function drawMesh () :Void {
        depthManager.reset();
        drawMeshs();
        depthManager.sort();
        depthManager.updateDepths();
    }

    private function drawLines () :Void {
        var len :Number = dataStorage.getLineListLength();
        for (var i :Number = 0; i < len; i++) {
            var index1 :Number = dataStorage.getLineIndex1(i);
            var index2 :Number = dataStorage.getLineIndex2(i);
            var v1 :Vector = dataStorage.getPointPosition(index1);
            var size1 :Number = 1;
            var v2 :Vector = dataStorage.getPointPosition(index2);
            var size2 :Number = 1;
            var sx1 :Number = paramControl.getScreenX(v1, size1);
            var sy1 :Number = paramControl.getScreenY(v1, size1);
            var sx2 :Number = paramControl.getScreenX(v2, size2);
            var sy2 :Number = paramControl.getScreenY(v2, size2);
            drawingControl.drawLine(sx1, sy1, sx2, sy2);
        }
    }

    private function drawMeshs () :Void {
        var len :Number = dataStorage.getLineListLength();
        for (var i :Number = 0; i < len; i++) {
            var index1 :Number = dataStorage.getMeshIndex1(i);
            var index2 :Number = dataStorage.getMeshIndex2(i);
            var index3 :Number = dataStorage.getMeshIndex3(i);
            var v1: Vector = dataStorage.getPointPosition(index1);
            var size1 :Number = 1;
            var v2 :Vector=dataStorage.getPointPosition(index2);
            var size2 :Number = 1;
            var v3 :Vector=dataStorage.getPointPosition(index3);
            var size3 :Number = 1;
            var sx1 :Number = paramControl.getScreenX(v1, size1);
            var sy1 :Number = paramControl.getScreenY(v1, size1);
            var sx2 :Number = paramControl.getScreenX(v2, size2);
            var sy2 :Number = paramControl.getScreenY(v2, size2);
            var sx3 :Number = paramControl.getScreenX(v3, size3);
            var sy3 :Number = paramControl.getScreenY(v3, size3);
            var nVector :Vector = dataStorage.getNormalVector(index1, index2, index3);
            var lVector :Vector = dataStorage.getLightVector();
            lVector.normalize();
            var cosign :Number = Math.abs(nVector.dotProduct(lVector));
            var mc :MovieClip = drawingControl.drawTriangleCos(sx1, sy1, sx2, sy2, sx3, sy3, cosign);
            var z :Number = Math.max(v1.z, Math.max(v2.z, v3.z));
            depthManager.addItem(z, mc);
        }
    }

    private function addMouseListener () :Void {
        var ref = this;
        path.createEmptyMovieClip("mouse_mc", MOUSE_MC_DEPTH);
        var mc :MovieClip = path.mouse_mc;
        mc._x = CENTER_X;
        mc._y = CENTER_Y;
        mc.onMouseUp = function () :Void {
            var obj :Object = new Object();
            obj.value = new Object();
            obj.value.x = this._xmouse;
            obj.value.y = this._ymouse;
            ref.onMouseUpHandler(obj);
        };
        mc.onMouseDown = function () :Void {
            var obj :Object = new Object();
            obj.value = new Object();
            obj.value.x = this._xmouse;
            obj.value.y = this._ymouse;
            ref.onMouseDownHandler(obj);
        };
    }

    private function removeMouseListener () :Void {
        var mc :MovieClip = path.mouse_mc;
        mc.onMouseUp = function () {
            //
        };
        mc.onMouseDown = function () {
            //
        };
        mc.removeMovieClip();
    }

    private function addMouseMoveListener () :Void {
        var ref = this;
        var mc :MovieClip = path.mouse_mc;
        mc.onMouseMove = function() :Void {
            var obj :Object = new Object();
            obj.value = new Object();
            obj.value.x = this._xmouse;
            obj.value.y = this._ymouse;
            ref.onMouseMoveHandler(obj);
        };
    }

    private function removeMouseMoveListener () :Void {
        var mc:MovieClip = path.mouse_mc;
        mc.onMouseMove = function () {
            //
        };
    }

    private function onMouseUpHandler (eventObj :Object) :Void {
        drawMesh();
        var x :Number = Number(eventObj.value.x);
        var y :Number = Number(eventObj.value.y);
        removeMouseMoveListener();
    }

    private function onMouseDownHandler (eventObj :Object) :Void {
        resetMesh();
        drawLine();
        var x :Number = Number(eventObj.value.x);
        var y :Number = Number(eventObj.value.y);
        oldX = x;
        oldY = y;
        addMouseMoveListener();
    }

    private function onMouseMoveHandler (eventObj :Object) :Void {
        var x :Number = Number(eventObj.value.x);
        var y :Number = Number(eventObj.value.y);
        var rotX :Number = -Math.PI * (y - oldY) / 200;
        var rotY :Number = Math.PI * (x - oldX) / 200;
        dataStorage.rotateX(rotX);
        dataStorage.rotateY(rotY);
        resetLine();
        drawLine();
        oldX = x;
        oldY = y;
    }

    public static function main () :Void {
        var structure :StructureControl = new StructureControl(_root);
    }
}

DepthManager.as
---
class DepthManager {
    private var itemList :Array;
    private var BACK_DEPTH :Number=900000;
    private var FRONT_DEPTH :Number=1000000;
    private var uniqIndex :Number;

    function DepthManager () {
    }

    public function reset () :Void{
        itemList = new Array();
        uniqIndex = 0;
    }

    public function addItem (z :Number, mc :MovieClip) :Void {
        var o :Object = new Object();
        var index :Number = uniqIndex;
        uniqIndex ++;
        o.z = z;
        o.mc = mc;
        o.index = index;
        itemList.push(o);
    }

    public function sort () :Void {
        itemList.sort(zCompare);
    }

    private function zCompare (a :Object, b :Object) :Number {
        var a_z :Number = Number(a.z);
        var a_uniq :Number = Number(a.index);
        var b_z = Number(b.z);
        var b_uniq :Number = Number(b.index);
        var ret :Number = a_z - b_z;
        if (ret == 0) {
            ret = a_uniq - b_uniq;
            if (ret == 0) {
                // OK ??
            }
        }
        return ret;
    }

    public function updateDepths () :Void {
        for (var i :Number = 0; i < itemList.length; i++) {
            var mc :MovieClip = MovieClip(itemList[i].mc);
            mc.swapDepths(BACK_DEPTH+i);
        }
    }
}

DrawingControl.as
---
class DrawingControl{
    private var path :MovieClip;
    private var triBase :MovieClip;
    private var lineBase :MovieClip;
    private var LINE_BASE_DEPTH :Number = 300;
    private var triDepth :Number;
    private var TRI_BASE_DEPTH :Number = 500;
    private var TRI_START_DEPTH :Number = 1;

    function DrawingControl (path :MovieClip) {
        this.path = path;
        path.createEmptyMovieClip("tri_base", TRI_BASE_DEPTH);
        triBase = path.tri_base;
        path.createEmptyMovieClip("line_base", LINE_BASE_DEPTH);
        lineBase = path.line_base;
        lineBase.lineStyle(0, 0x000000);
        triDepth = TRI_START_DEPTH;
    }

    public function drawTriangle (x1 :Number, y1 :Number, x2 :Number, y2 :Number, x3 :Number, y3 :Number) :MovieClip {
        var depth :Number = triDepth;
        triDepth ++;
        var mcname :String = "tri" + String(depth);
        triBase.createEmptyMovieClip(mcname, depth);
        var mc :MovieClip = triBase[mcname];
        mc.lineStyle(1, 0x000000);
        mc.beginFill(0xFF0000, 100);
        mc.moveTo(x1, y1);
        mc.lineTo(x2, y2);
        mc.lineTo(x3, y3);
        mc.endFill();
        return mc;
    }

    public function drawTriangleCos (x1 :Number, y1 :Number, x2 :Number, y2 :Number, x3 :Number, y3 :Number, cosign :Number) :MovieClip {
        var depth :Number = triDepth;
        triDepth ++;
        var mcname :String = "tri" + String(depth);
        triBase.createEmptyMovieClip(mcname, depth);
        var mc :MovieClip = triBase[mcname];
        var color16 :Number = Math.floor(256 * cosign) << 16;
        mc.beginFill(color16, 100);
        mc.moveTo(x1, y1);
        mc.lineTo(x2, y2);
        mc.lineTo(x3, y3);
        mc.endFill();
        return mc;
    }

    public function drawLine (x1 :Number, y1 :Number, x2 :Number, y2 :Number) :Void {
        var mc :MovieClip = lineBase;
        mc.moveTo(x1, y1);
        mc.lineTo(x2, y2);
    }

    public function removeAllTriangles () :Void {
        triBase.removeMovieClip();
        path.createEmptyMovieClip("tri_base", TRI_BASE_DEPTH);
        triBase = path.tri_base;
        triDepth = TRI_START_DEPTH;
    }

    public function removeAllLines () :Void {
        lineBase.removeMovieClip();
        path.createEmptyMovieClip("line_base", LINE_BASE_DEPTH);
        lineBase = path.line_base;
        lineBase.lineStyle(0, 0x000000);
    }
}

ModelDataStorage.as
---
class ModelDataStorage extends RotateDataStorage{
    private var pointList :Array;
    private var lineList :Array;
    private var meshList :Array;
    private var LIGHT_X :Number = 3;
    private var LIGHT_Y :Number = 4;
    private var LIGHT_Z :Number = -23;
    private var pointAddress :String;
    private var lineAddress :String;
    private var meshAddress :String;

    function ModelDataStorage () {
        reset();
    }

    public function reset () :Void {
        pointList = new Array();
        lineList = new Array();
        meshList = new Array();
        pointAddress = "";
        lineAddress = "";
        meshAddress = "";
    }

    public function getPointListLength () :Number {
        return pointList.length;
    }

    public function getLineListLength () :Number {
        return lineList.length;
    }

    public function getMeshListLength () :Number {
        return meshList.length;
    }

    public function addPoint (id :String, x :Number, y :Number, z :Number) :Void {
        var o :Object = new Object();
        o.id = id;
        o.x = x;
        o.y = y;
        o.z = z;
        pointList.push(o);
    }

    public function setPointPosition (index :Number, x :Number, y :Number, z :Number) :Void {
        pointList[index].x = x;
        pointList[index].y = y;
        pointList[index].z = z;
    }

    public function getPointId (index :Number) :String {
        return String(pointList[index].id);
    }

    public function getPointPosX (index :Number) :Number {
        return Number(pointList[index].x);
    }

    public function getPointPosY (index :Number) :Number {
        return Number(pointList[index].y);
    }

    public function getPointPosZ (index :Number) :Number {
        return Number(pointList[index].z);
    }

    public function getPointPosition (index :Number) :Vector {
        var v :Vector = new Vector();
        v.x = Number(pointList[index].x);
        v.y = Number(pointList[index].y);
        v.z = Number(pointList[index].z);
        return v;
    }

    public function addLine (id :String, point1 :String, point2 :String) :Void {
        var o:Object = new Object();
        o.id = id;
        o.point1 = point1;
        o.point2 = point2;
        lineList.push(o);
    }

    public function getLineIndex1 (index :Number) :Number {
        return Number(lineList[index].index1);
    }

    public function getLineIndex2 (index :Number) :Number {
        return Number(lineList[index].index2);
    }

    public function addPointIndexToLineLists () :Void {
        var len:Number=getLineListLength();
        for(var i :Number = 0; i < len; i++) {
            addPointIndexToLine(i);
        }
    }

    private function addPointIndexToLine (index :Number) :Void {
        var point1 :String = String(lineList[index].point1);
        var point2 :String = String(lineList[index].point2);
        var pointIndex1 :Number = getPointIndexFromId(point1);
        var pointIndex2 :Number = getPointIndexFromId(point2);
        lineList[index].index1 = pointIndex1;
        lineList[index].index2 = pointIndex2;
    }

    public function addMesh (id :String, point1 :String, point2 :String, point3 :String) :Void {
        var o :Object = new Object();
        o.id = id;
        o.point1 = point1;
        o.point2 = point2;
        o.point3 = point3;
        meshList.push(o);
    }

    public function getMeshIndex1 (index :Number) :Number {
        return Number(meshList[index].index1);
    }

    public function getMeshIndex2 (index :Number) :Number {
        return Number(meshList[index].index2);
    }

    public function getMeshIndex3 (index :Number) :Number {
        return Number(meshList[index].index3);
    }

    public function addPointIndexToMeshLists () :Void {
        var len :Number = getMeshListLength();
        for(var i :Number = 0; i < len; i++) {
            addPointIndexToMesh(i);
        }
    }

    private function addPointIndexToMesh (index :Number) :Void {
        var point1 :String = String(meshList[index].point1);
        var point2 :String = String(meshList[index].point2);
        var point3 :String = String(meshList[index].point3);
        var pointIndex1 :Number = getPointIndexFromId(point1);
        var pointIndex2 :Number = getPointIndexFromId(point2);
        var pointIndex3 :Number = getPointIndexFromId(point3);
        meshList[index].index1 = pointIndex1;
        meshList[index].index2 = pointIndex2;
        meshList[index].index3 = pointIndex3;
    }

    private function getPointIndexFromId (id :String) :Number {
        var ret :Number;
        for(var i :Number = 0; i < pointList.length; i++) {
            var targetId :String = String(pointList[i].id);
            if (id == targetId) {
                ret = i;
                break;
            }
        }
        return ret;
    }

    private function getGravityX () :Number {
        var len :Number = getPointListLength();
        var ret :Number = 0;
        for(var i :Number = 0; i < len; i++) {
            ret += Number(pointList[i].x);
        }
        return ret / len;
    }

    private function getGravityY () :Number {
        var len :Number = getPointListLength();
        var ret :Number = 0;
        for (var i :Number = 0; i < len; i++){
            ret += Number(pointList[i].y);
        }
        return ret / len;
    }

    private function getGravityZ () :Number {
        var len :Number = getPointListLength();
        var ret :Number = 0;
        for (var i :Number = 0; i < len; i++){
            ret += Number(pointList[i].z);
        }
        return ret / len;
    }

    public function centering () :Void {
        var len :Number = getPointListLength();
        var cx :Number = getGravityX();
        var cy :Number = getGravityY();
        var cz :Number = getGravityZ();
        for(var i :Number = 0; i < len; i++) {
            pointList[i].x -= cx;
            pointList[i].y -= cy;
            pointList[i].z -= cz;
        }
    }

    public function getNormalVector (index1 :Number, index2 :Number, index3 :Number) :Vector {
        var v1 :Vector = getPointPosition(index1);
        var v2 :Vector = getPointPosition(index2);
        var v3 :Vector = getPointPosition(index3);

        var a :Vector = new Vector();
        var b :Vector = new Vector();

        a.x = v1.x - v2.x;
        a.y = v1.y - v2.y;
        a.z = v1.z - v2.z;
        b.x = v1.x - v3.x;
        b.y = v1.y - v3.y;
        b.z = v1.z - v3.z;

        var n :Vector = new Vector();
        n.crossProduct(a, b);
        n.normalize();
        return n;
    }

    public function getLightVector () :Vector {
        var l :Vector = new Vector();
        l.x = LIGHT_X;
        l.y = LIGHT_Y;
        l.z = LIGHT_Z;
        return l;
    }

    public function setPointAddress (address :String) :Void {
        pointAddress = address;
    }

    public function getPointAddress () :String {
        return pointAddress;
    }

    public function setLineAddress (address :String) :Void {
        lineAddress = address;
    }

    public function getLineAddress () :String {
        return lineAddress;
    }

    public function setMeshAddress (address :String) :Void {
        meshAddress = address;
    }

    public function getMeshAddress () :String {
        return meshAddress;
    }
}

ParamControl.as
---
class ParamControl{
    private var cx :Number;
    private var cy :Number;
    private var cz :Number;
    private var screenLength :Number;
    private var defaultLength :Number;
    private var scale :Number;
    private var defaultScale :Number;

    function ParamControl () {
    }

    public function getScreenX (v :Vector, size :Number) :Number {
        return scale * (v.x - cx) * screenLength / (v.z - cz);
    }

    public function getScreenY (v :Vector, size :Number) :Number{
        return scale * (v.y - cy) * screenLength / (v.z - cz);
    }

    public function getScreenSize (x :Number, y :Number, z :Number, size :Number) :Number {
        return Math.abs(scale * size * screenLength / (z - cz));
    }

    public function setCenterPosition (x :Number, y :Number, z :Number) :Void {
        cx = x;
        cy = y;
        cz = z;
    }

    public function setScale (scale :Number) :Void {
        this.scale = scale;
    }

    public function setDefaultScale (scale :Number) :Void {
        defaultScale = scale;
        resetScale();
    }

    public function resetScale () :Void {
        scale = defaultScale;
    }

    public function setScreenLength (length :Number) :Void {
        screenLength = length;
    }

    public function setDefaultLength (length :Number) :Void {
        defaultLength = length;
        resetScreenLength();
    }

    public function resetScreenLength () :Void {
        scale = defaultLength;
    }
}

RotateDataStorage.as
---
class RotateDataStorage{
    public var getPointListLength :Function;
    public var getPointPosX :Function;
    public var getPointPosY :Function;
    public var getPointPosZ :Function;
    public var setPointPosition :Function;

    function RotateDataStorage () {
    }

    public function rotateX (theta :Number) :Void {
        var len :Number = getPointListLength();
        for(var i :Number = 0; i < len; i++) {
            var old_x :Number = getPointPosX(i);
            var old_y :Number = getPointPosY(i);
            var old_z :Number = getPointPosZ(i);
            var new_x :Number = old_x;
            var new_y :Number = old_y * Math.cos(theta) + old_z * Math.sin(theta);
            var new_z :Number = -old_y * Math.sin(theta) + old_z * Math.cos(theta);
            setPointPosition(i, new_x, new_y, new_z);
        }
    }

    public function rotateY (theta :Number) :Void {
        var len :Number = getPointListLength();
        for(var i :Number = 0; i < len; i++) {
            var old_x :Number = getPointPosX(i);
            var old_y :Number = getPointPosY(i);
            var old_z :Number = getPointPosZ(i);
            var new_x :Number = old_x * Math.cos(theta) - old_z * Math.sin(theta);
            var new_y :Number = old_y;
            var new_z :Number = old_x * Math.sin(theta) + old_z * Math.cos(theta);
            setPointPosition(i, new_x, new_y, new_z);
        }
    }

    public function rotateZ (theta :Number) :Void {
        var len :Number = getPointListLength();
        for(var i :Number = 0; i < len; i++) {
            var old_x :Number = getPointPosX(i);
            var old_y :Number = getPointPosY(i);
            var old_z :Number = getPointPosZ(i);
            var new_x :Number = old_x * Math.cos(theta) + old_y * Math.sin(theta);
            var new_y :Number = -old_x * Math.sin(theta) + old_y * Math.cos(theta);
            var new_z :Number = old_z;
            setPointPosition(i, new_x, new_y, new_z);
        }
    }
}

Vector.as
---
class Vector{
    public var x :Number;
    public var y :Number;
    public var z :Number;

    function Vector () {
        x = null;
        y = null;
        z = null;
    }

    public function normalize () :Void {
        var r :Number = Math.sqrt(x * x + y * y + z * z);
        x /= r;
        y /= r;
        z /= r;
    }

    public function dotProduct (v :Vector) :Number {
        return x * v.x + y * v.y + z * v.z;
    }

    public function crossProduct (a :Vector, b :Vector) :Void {
        x = a.y * b.z - a.z * b.y;
        y = a.z * b.x - a.x * b.z;
        z = a.x * b.y - a.y * b.x;
    }
}

XMLIOService.as
---
class XMLIOService{
    private var loader :XML;
    private var handlers :Array;

    function XMLIOService () {
    }

    public function reset () :Void {
        var ref = this;
        handlers = new Array();
        loader = new XML();
        loader.ignoreWhite=true;
        loader.onLoad = function () :Void {
            ref.onLoadComplete();
        };
    }

    public function load (address :String, sendObj :Object, flag :Boolean) :Void {
        var now :Date = new Date();
        var rnd :Number = now.getTime();
        var addr :String = address;
        var str :String = "";
        for (var key :String in sendObj) {
            var val :String = String(sendObj[key]);
            str += key + "=" + val + "&";
        }

        if (str.length > 0) {
            addr += "?" + str;
        }
        if (flag) {
            addr += "rnd=" + String(rnd) + "&";
        }
        loader.load(addr);
    }

    public function addLoadComplete(method :String, ref :Object) :Void {
        var o :Object = new Object();
        o.method = method;
        o.ref = ref;
        handlers.push(o);
    }

    private function onLoadComplete () :Void {
        for (var i :Number = 0; i < handlers.length; i++) {
            var method :String = String(handlers[i].method);
            var ref :Object = Object(handlers[i].ref);
            ref[method](loader);
        }
    }
}

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





All Contents Copyright (C) 2005 HOSHI Tetsuya
Home