一个3Dbox

      源代码 2005-4-20 11:47
一个3Dbox(jame.li)
源代码:
//DEVELOPED BY JAMES.LI 2005/1/25//
//PRIVATE CODES//
#initclip
function james3DClass() {
    this.init();
}
Object.registerClass("james3DCom", james3DClass);
james3DClass.prototype = new MovieClip();
var proto = james3DClass.prototype;
//进入程序//
proto.init = function() {
    //init public//
    this.setFriction(this.friction);
    //
    //建立平面//
    this.createSides(this.outs, this.inters);
    //初始化各项参数//
    this.replaceSides(this.angleXY);
    //设置控制//
    this.startCtrl(this.mouseCtrl);
};
//*************************************************建立平面函数组****************************//
proto.createSides = function(arr, arr2) {
    this.createEmptyMovieClip("sides", 0);
    this.sides._xscale = this.sides._yscale=this.scale;
    for (var i = 0; i<6; i++) {
        this.sides.createEmptyMovieClip("side"+i, i);
        this.sides["side"+i].createEmptyMovieClip("inside0", 0);
        this.sides["side"+i].inside0.createEmptyMovieClip("inside1", 0);
        this.sides["side"+i].inside0.inside1.createEmptyMovieClip("inside2", 0);
        this.sides["side"+i].inside0.inside1.inside2.attachMovie(arr[i], "pic", 0, {id:i});
        if (arr2[i] != undefined) {
            this.sides["side"+i].inside0.inside1.inside2.attachMovie(arr2[i], "pic2", 1, {id:i,_xscale:-100});
        }
    }
    //获取离心距//
    for (var i = 0; i<6; i++) {
        if (this.lens[i] == undefined || typeof (this.lens[i]) != "number") {
            this.sides["side"+i].dis = 1;
        } else {
            this.sides["side"+i].dis = Math.abs(this.lens[i]);
        }
    }
};
//************************************************初始化部分********************************//
proto.replaceSides = function(axy) {
    //初始化平面参数//
    this.initSides();
    //初始化运动速度//
    this.releaseX = axy[0];
    this.releaseY = axy[1];
    //初次阻尼运动//
    this.frictionMove();
};
proto.initSides = function() {
    for (var i = 0; i<6; i++) {
        var side = this.sides["side"+i];
        var posmc = side.inside0;
        switch (i) {
        case 0 :
            //参数解释:x,y为坐标的精确计算变量.os为对立面.dep为上层优先级.front为布尔值,表示是否正面向上//
            posmc._x = posmc._y=0;
            posmc.x = posmc._x;
            posmc.y = posmc._y;
            side.os = this.sides.side5;
            side.dep = 2;
            side.front = true;
            break;
        case 1 :
            posmc._x = side.dis;
            posmc._y = 0;
            posmc.x = posmc._x;
            posmc.y = posmc._y;
            side.os = this.sides.side4;
            side.dep = 1;
            side.front = true;
            break;
        case 2 :
            posmc._x = 0;
            posmc._y = -side.dis;
            posmc.x = posmc._x;
            posmc.y = posmc._y;
            side.os = this.sides.side3;
            side.dep = 0;
            side.front = true;
            break;
        case 3 :
            posmc._x = 0;
            posmc._y = side.dis;
            posmc.x = posmc._x;
            posmc.y = posmc._y;
            side.os = this.sides.side2;
            side.dep = 0;
            side.front = false;
            break;
        case 4 :
            posmc._x = -side.dis;
            posmc._y = 0;
            posmc.x = posmc._x;
            posmc.y = posmc._y;
            side.front = false;
            side.dep = 1;
            side.os = this.sides.side1;
            break;
        case 5 :
            posmc._x = posmc._y=0;
            posmc.x = posmc._x;
            posmc.y = posmc._y;
            side.os = this.sides.side0;
            side.dep = 2;
            side.front = false;
            break;
        }
    }
};
//****************************************************刷新部分************************//
proto.updateShower = function(axy) {
    //刷新驱动面中心点坐标//
    for (var i = 0; i<6; i++) {
        var side = this.sides["side"+i];
        var posmc = side.inside0;
        if (i<3) {
            this.updateDotPosition(side, posmc, axy[0], axy[1]);
        }
        this.setLight(side, posmc);
    }
    //刷新驱动面透视显示以及传动系统//
    for (var i = 0; i<6; i++) {
        var side = this.sides["side"+i];
        var posmc = side.inside0;
        if (i<3) {
            this.updateRotation(side);
        } else {
            var tarside = side.os;
            var tarposmc = tarside.inside0;
            posmc.x = -tarposmc.x*(side.dis/tarside.dis);
            posmc.y = -tarposmc.y*(side.dis/tarside.dis);
            posmc._x = posmc.x;
            posmc._y = posmc.y;
            posmc.inside1._xscale = tarposmc.inside1._xscale;
            posmc.inside1.inside2.pic._xscale = -tarposmc.inside1.inside2.pic._xscale;
            posmc.inside1.inside2.pic._rotation = tarposmc.inside1.inside2.pic._rotation;
            posmc.inside1.inside2.pic2._xscale = -tarposmc.inside1.inside2.pic2._xscale;
            posmc.inside1.inside2.pic2._rotation = tarposmc.inside1.inside2.pic2._rotation;
            posmc.inside1._rotation = tarposmc.inside1._rotation;
            posmc.inside1.inside2._rotation = tarposmc.inside1.inside2._rotation;
            side.front = !tarside.front;
        }
        //刷新层显示//
        this.updateDepths(side, i);
    }
};
proto.setLight = function(side, posmc) {
    var pic = side.inside0.inside1.inside2.pic;
    var pic1 = side.inside0.inside1.inside2.pic2;
    var angle = this.lightAngle*(Math.PI/180);
    var dis = this.lightDis;
    var x = this.lightDis*Math.cos(angle)*1/2;
    var y = this.lightDis*Math.sin(angle)*1/2;
    var x1 = -this.lightDis*Math.cos(angle)*1/2;
    var y1 = -this.lightDis*Math.sin(angle)*1/2;
    if (this.lightEffect) {
        this.setColor(pic, side, posmc, -200, [x, y], 0);
        this.setColor(pic1, side, posmc, -200, [x1, y1], -50);
    } else {
        this.setColor(pic, side, posmc, 0, [x, y], 0);
        this.setColor(pic1, side, posmc, 0, [x1, y1], -50);
    }
};
proto.setColor = function(mc, side, posmc, light, arr, p) {
    var mycolor = new Color(mc);
    var c = new Object();
    var x = arr[0];
    var y = arr[1];
    var dx = posmc.x-x;
    var dy = posmc.y-y;
    var lvl = Math.sqrt(dx*dx+dy*dy);
    c.rb = c.gb=c.bb=lvl*light/(2*side.dis)+p;
    c.ra = c.ga=c.ba=100;
    c.aa = 100;
    c.ab = 100;
    mycolor.setTransform(c);
};
proto.updateDepths = function(side, id) {
    if (side.front) {
        side.swapDepths(10+side.dep);
        side.inside0.inside1.inside2.pic2._visible = false;
        side.inside0.inside1.inside2.pic._visible = true;
    } else {
        side.swapDepths(id);
        side.inside0.inside1.inside2.pic2._visible = true;
        side.inside0.inside1.inside2.pic._visible = false;
    }
};
//主算法一:坐标计算//
proto.updateDotPosition = function(side, posmc, vax, vay) {
    var limit = 50;
    var vx = Math.max(-limit, Math.min(limit, vax));
    var vy = Math.max(-limit, Math.min(limit, vay));
    this.releaseX = vx;
    this.releaseY = vy;
    this.updateXandY(["x", "y"], side, posmc, vx);
    this.updateXandY(["y", "x"], side, posmc, vy);
    var dis = Math.sqrt(Math.pow(posmc.x, 2)+Math.pow(posmc.y, 2));
    var outa;
    var cap;
    if (side.front) {
        outa = Math.atan2(posmc.y, posmc.x);
        cap = 1;
    } else {
        outa = Math.atan2(-posmc.y, -posmc.x);
        cap = -1;
    }
    posmc.inside1.inside2._rotation = -cap*outa*(180/Math.PI);
    posmc.inside1._xscale = cap*100*Math.cos(Math.asin(dis/side.dis));
    posmc.inside1._rotation = outa*(180/Math.PI);
};
proto.updateRotation = function(side) {
    var posmc = side.inside0;
    var romc = posmc.inside1.inside2.pic;
    var romc2 = posmc.inside1.inside2.pic2;
    var vmc;
    var hmc;
    if (romc.id == 0) {
        vmc = this.sides.side2.inside0;
    } else if (romc.id == 1) {
        vmc = this.sides.side2.inside0;
    } else if (romc.id == 2) {
        vmc = this.sides.side0.inside0;
    }
    var k;
    if (posmc.x == 0) {
        k = 0;
    } else {
        k = -posmc.y/posmc.x;
    }
    var d = -vmc.y-vmc.x*k;
    var r = Math.abs(vmc._parent.dis);
    //var r = 50;
    var a = 1+k*k;
    var b = 2*k*d;
    var c = d*d-r*r;
    var delt = b*b-4*a*c;
    var x;
    //var just = hk*(-vmc.y/vmc.x);
    var just = this.getAngle([posmc.x, posmc.y], [0, 0], [vmc.x, vmc.y]);
    if (posmc.x>0) {
        if (just>90) {
            x = (-b-Math.sqrt(delt))/(2*a);
        } else {
            x = (-b+Math.sqrt(delt))/(2*a);
        }
    } else {
        if (just>90) {
            x = (-b+Math.sqrt(delt))/(2*a);
        } else {
            x = (-b-Math.sqrt(delt))/(2*a);
        }
    }
    var y = -(k*x+d);
    var ro = Math.atan2(x, y)*(180/Math.PI)-180;
    if (!side.front) {
        ro = -ro;
    }
    romc._rotation = -ro;
    romc2._rotation = -ro;
};
proto.getDistance = function(x1, x2, y1, y2) {
    return Math.abs(Math.sqrt(Math.pow(x1-x2, 2)+Math.pow(y1-y2, 2)));
};
proto.getAngle = function(arra, arrc, arrb) {
    var a = this.getDistance(arrc[0], arrb[0], arrc[1], arrb[1]);
    var b = this.getDistance(arra[0], arrc[0], arra[1], arrc[1]);
    var c = this.getDistance(arra[0], arrb[0], arra[1], arrb[1]);
    //trace(a*b)
    var angleC = 180-180*Math.acos((Math.pow(c, 2)-Math.pow(a, 2)-Math.pow(b, 2))/(2*a*b))/Math.PI;
    return angleC;
};
proto.updateXandY = function(arr, side, mc, va) {
    var radius = Math.sqrt(Math.pow(side.dis, 2)-Math.pow(mc[arr[1]], 2));
    var rotate;
    var angle = Math.asin(mc[arr[0]]/radius)*(180/Math.PI);
    if (radius == 0) {
        angle = 0;
    }
    rotate = angle;
    if (side.front) {
        rotate = angle;
    } else {
        rotate = 180-angle;
    }
    var old = rotate;
    rotate += va;
    if (rotate>180) {
        rotate -= 360;
    } else if (rotate<-180) {
        rotate += 360;
    }
    if (side.front) {
        if (rotate>90 || rotate<-90) {
            side.front = false;
        }
    } else {
        if (rotate<90 && rotate>-90) {
            side.front = true;
        }
    }
    var savePos = mc[arr[0]];
    mc[arr[0]] = mc["_"+arr[0]]=radius*Math.sin(rotate*(Math.PI/180));
    mc["_r"+arr[0]] = rotate*(Math.PI/180);
};
//mouse//
proto.startCtrl = function(info) {
    if (info == "none") {
        clearInterval(this.releaseInterval);
        this.frictionMove();
    } else if (info == "drag") {
        this.onMouseDown = function() {
            clearInterval(this.releaseInterval);
            this.startX = _xmouse;
            this.startY = _ymouse;
            this.onMouseMove = function() {
                updateAfterEvent();
                this.releaseX = (_xmouse-this.startX);
                this.releaseY = (_ymouse-this.startY);
                this.updateShower([this.releaseX, this.releaseY]);
                this.startX = _xmouse;
                this.startY = _ymouse;
            };
        };
        this.onMouseUp = function() {
            this.frictionMove();
            delete this.onMouseMove;
        };
    } else if (this.mouseCtrl == "auto") {
    }
};
//friction//
proto.frictionMove = function() {
    this.releaseInterval = setInterval(this.released, 10, this);
};
proto.released = function(obj) {
    updateAfterEvent();
    obj.updateShower([obj.releaseX, obj.releaseY]);
    obj.releaseX *= (1-obj.friction);
    obj.releaseY *= (1-obj.friction);
    if (Math.abs(obj.releaseX)<0.1 && Math.abs(obj.releaseY)<0.1) {
        clearInterval(obj.releaseInterval);
    }
};
//public//
proto.setFriction = function(num) {
    var n = Number(num);
    if (n<0) {
        n = 0;
    } else if (n>1) {
        n = 1;
    }
    this.friction = n;
};
proto.getFriction = function() {
    return this.friction;
};
proto.setSideDis = function(sideid, dis) {
    var d = Math.abs(Number(dis));
    this["side"+sideid].dis = d;
};
proto.getSideDis = function(sideid) {
    return this["side"+sideid].dis;
};
proto.setMouseCtrl = function(info) {
    if (info == "none" || info == "drag") {
        this.startCtrl(info);
        this.mouseCtrl = info;
    }
};
proto.getMouseCtrl = function() {
    return this.mouseCtrl;
};
proto.setMoveArray = function(arr) {
    this.releaseX = Number(arr[0]);
    this.releaseY = Number(arr[1]);
};
proto.getMoveArray = function() {
    return [this.releaseX, this.releaseY];
};
proto.getSide = function(sideid) {
    return this["side"+sideid];
};
proto.setLightEffect = function(flag) {
    this.lightEffect = Boolean(flag);
};
proto.getLightEffect = function() {
    return this.lightEffect;
};
#endinitclip
标签集:TAGS:
回复Comments() 点击Count()

回复Comments

{commentauthor}
{commentauthor}
{commenttime}
{commentnum}
{commentcontent}
作者:
{commentrecontent}