flash on 2012-2-29

by tepe
♥0 | Line 131 | Modified 2012-03-02 10:49:47 | MIT License
play

ActionScript3 source code

/**
 * Copyright tepe ( http://wonderfl.net/user/tepe )
 * MIT License ( http://www.opensource.org/licenses/mit-license.php )
 * Downloaded from: http://wonderfl.net/c/kOjX
 */

package {
    import flash.display.Sprite;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;
    public class FlashTest extends Sprite {
        private var myCanvas:Sprite = new Sprite();
        private var tf1:TextField = new TextField();
        //private var 
        public function FlashTest() {
            // write as3 code here..
            addEventListener(Event.ENTER_FRAME,onEnter);
            stage.addEventListener(KeyboardEvent.KEY_DOWN,onKey);
            with(myCanvas){
                x = 200;
                y = 200;
                graphics.lineStyle(1,0x000000);
                graphics.beginFill(0x00ff00);
                graphics.drawRect(-100,-100,200,200);
                graphics.endFill();
                graphics.beginFill(0xff0000);
                graphics.drawRect( 100,100,200,200);
                graphics.endFill();
                graphics.moveTo(0,-200);
                graphics.lineTo(0,200);
                graphics.moveTo(-200,0);
                graphics.lineTo(200,0);
                
            }
            addChild(tf1);
            addChild(myCanvas);
            

        }
        private var rX:Number = 0;
        private var rY:Number = 0;
        private var rZ:Number = 0;
        private var pX:Number = 200,
                    pY:Number = 200,
                    pZ:Number = 0;
       /* private function init():void{
            var t:Number = 10;
            var cos:Number = Math.cos(t / 2);
            var sin:Number = Math.sin(t / 2);
            var r:Quaternion = new Quaternion(cos, v.x * sin, v.y * sin, v.z * sin);
            var q:Quaternion = new Quaternion(cos,-v.x * sin,-v.y * sin,-v.z * sin);
        }*/


        private function onKey(e:KeyboardEvent):void{
            tf1.text = e.keyCode.toString();
            if(e.keyCode == 65){
                rY-=0.1;
            }
            if(e.keyCode == 83){
                rY+=0.1;
            }
            if(e.keyCode == 87){
                rX+=0.1;
            }

            if(e.keyCode == 90){
                rX-=0.1;
            }
            if(e.keyCode == 81){
                rZ-=0.1;
            }

            if(e.keyCode == 69){
                rZ+=0.1;
            }
            //矢印キー
            if(e.keyCode == 37){// ←
                pX--;
                //myCanvas.x--;
            }
            if(e.keyCode == 38){// ↑
                pZ++;
                //myCanvas.z++;
            }
            if(e.keyCode == 39){// →
                pX++;
                //myCanvas.x++;
            }
            if(e.keyCode == 40){// ↓
                pZ--;
                //myCanvas.z--;
            }




        }

        private var nRotationX:Number = 0;
        private var nRotationY:Number = 0;
        private var nRotationZ:Number = 0;
        private function onEnter(e:Event):void
        {
            var mat3D:Matrix3D = new Matrix3D();
            //tf1.text = myCanvas.mouseX.toString()+ " "+myCanvas.mouseY.toString();
            //tf1.appendText("\n"+nRotationZ.toString());
            nRotationX+=rX;    
            nRotationY+=rY;    
            nRotationZ+=rZ;

            //回転中心点をずらす
            //mat3D.appendTranslation(-myCanvas.width/2,-myCanvas.height/2,0);

            //回転
            mat3D.appendRotation(nRotationX, Vector3D.X_AXIS);
            mat3D.appendRotation(nRotationY, Vector3D.Y_AXIS);
            mat3D.appendRotation(nRotationZ, Vector3D.Z_AXIS);

            //中心点をもどす
           // mat3D.appendTranslation(myCanvas.width/2,myCanvas.height/2,0);
    
            //Matrix3Dを適応
            myCanvas.transform.matrix3D = mat3D;
            myCanvas.x = pX;
            myCanvas.y = pY;
            myCanvas.z = pZ;
        }
    }
}




//-----------------------------------
class Point3D {
        public var x:Number;
        public var y:Number;
        public var z:Number;
        public function Point3D(x:Number, y:Number, z:Number) {
            this.x = x;
            this.y = y;
            this.z = z;
        }
        public function distance():Number {
            return Math.sqrt(x * x + y * y + z * z);
        }
        public function normalize():Point3D {
            var d:Number = distance();
            return new Point3D(x / d, y / d, z / d);
        }
        public function sub(p:Point3D):Point3D {
            return new Point3D(x - p.x, y - p.y, z - p.z);
        }
        public function outerProduct(p:Point3D):Point3D {
            return new Point3D(y * p.z - z * p.y, z * p.x - x * p.z, x * p.y - y * p.x);
        }
        public function innerProduct(p:Point3D):Number {
            return x * p.x + y * p.y + z * p.z;
        }
    }

//^------------------------------------

class Quaternion {
        public var t:Number;
        public var x:Number;
        public var y:Number;
        public var z:Number;
        /** 4つの成分を指定してクォータニオンを構築 */
        public function Quaternion(t:Number, x:Number, y:Number, z:Number) {
            this.t = t;
            this.x = x;
            this.y = y;
            this.z = z;
        }
        /** かけ算 */
        public function product(q:Quaternion):Quaternion {
            return new Quaternion(t * q.t - x * q.x - y * q.y - z * q.z,
                                  t * q.x + x * q.t + y * q.z - z * q.y,
                                  t * q.y + y * q.t + z * q.x - x * q.z,
                                  t * q.z + z * q.t + x * q.y - y * q.x);
        }
    }