尔冬橙

博客园 首页 新随笔 联系 订阅 管理

package
{
    import base.*;
    
    import flare.basic.*;
    import flare.core.*;
    import flare.loaders.*;
    import flare.system.*;
    import flare.utils.*;
    
    import flash.display.*;
    import flash.events.*;
    
    [SWF(frameRate = 60,width = 800,height = 450,backgroundColor = 0x000000)]
    
    public class TheBasic1 extends Base
    {
        private var scene:Scene3D;
        //小车模型
        private var car:Pivot3D;
        private var carSpeed:Number = 0;
        private var carRotation:Number = 0;
        
        //小车的四个轮子 
        private var whell1:Pivot3D;
        private var whell2:Pivot3D;
        private var whell3:Pivot3D;
        private var whell4:Pivot3D;
        private var whellRotation:Number = 0;
        //坐标模型
        //private var axis:Pivot3D;
        //地面
        private var floor:Pivot3D;
        
        public function TheBasic1()
        {
            super("小车");
            //舞台设置
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
            
            //创建3D场景
            scene = new Viewer3D(this);
            //抗锯齿
            scene.antialias = 2;
            
            //添加全局的进度和完成事件处理
            scene.addEventListener(Scene3D.PROGRESS_EVENT,progressEvent);
            scene.addEventListener(Scene3D.COMPLETE_EVENT,completeEvent);
            
            //如果我们不创建相机,flare3D将使用在加载的模型中发现的第一个相机。
            //如果相机是随着加载的模型一起加载,模型作为相机的父对象。如果我们移动模型,相机也随之移动。
            //这样将看不出移动。为了避免这样的问题,我们创建并管理一个自己的相机。            
            scene.camera = new Camera3D("myOwnCamera");
            //我们能像操作其他模型对象一样操作相机
            scene.camera.setPosition(20, 20, 20 );
            scene.camera.lookAt(0,0,0);
            
            //加载对象
            car = scene.addChildFromFile("../resources/car.f3d");        
            //axis = scene.addChildFromFile("../resources/axis.f3d");
            floor = scene.addChildFromFile("../resources/plane.f3d");
            //避免边加载边渲染,全部加载完再渲染
            scene.pause();
            //开始更新场景。
            scene.addEventListener(Scene3D.UPDATE_EVENT,updateEvent);
        }
        private function progressEvent(e:Event):void
        {
            trace(scene.loadProgress);
        }
        
        private function completeEvent(e:Event):void
        {
            trace("load completed");
            //axis.setScale(0.5,0.5,0.5);
            //场景加载完毕开始恢复渲染。
            scene.resume();
            //停止汽车的动画
            car.stop();
            //通过名字得到4个轮子
            whell1 = car.getChildByName("AXTruck-FLWheel");
            whell2 = car.getChildByName("AXTruck-FLWheel001");
            //后轮
            whell3 = car.getChildByName("AXTruck-BLWheel");
            whell4 = car.getChildByName("AXTruck-BLWheel001");
            
            //开始更新场景
            scene.addEventListener(Scene3D.UPDATE_EVENT,updateEvent);
        }
        private function updateEvent(e:Event):void
        {
            //按UP和Down,沿着Z轴移动。
            if(Input3D.keyDown(Input3D.UP)) carSpeed += 0.1;
            if(Input3D.keyDown(Input3D.DOWN)) carSpeed -= 0.1;
            
            //沿着Y轴旋转
            if(Input3D.keyDown(Input3D.LEFT)) carRotation -= 6;
            if(Input3D.keyDown(Input3D.RIGHT)) carRotation += 6;
            
            //应用一些因子逐渐降低速度和旋转。
            carSpeed *=0.9;
            carRotation *=0.9;
            
            //增加随X轴的旋转。
            whellRotation = carSpeed * 10;
            //对前轮使用绝对旋转。
            whell1.setRotation(whellRotation,carRotation,0);
            whell2.setRotation(whellRotation,carRotation,0);
            
            //对后轮使用本地旋转。
            whell3.rotateX(carSpeed * 8);
            whell4.rotateX(carSpeed * 8);
            
            //车沿着Y轴旋转。
            car.rotateY(carRotation * carSpeed /20);
            
            //车向前走。
            car.translateZ(carSpeed);
            
            //重置
            if(Input3D.keyDown(Input3D.R))
            {
                car.setPosition(0,0,0);
                car.setRotation(0,0,0);
            }

            Pivot3DUtils.setPositionWithReference(scene.camera,0,10,-10,car,0.1);
            
            Pivot3DUtils.lookAtWithReference(scene.camera,0,5,0,car,car.getDir(),0.05);
                
        }
    }
}

 

posted on 2012-08-27 11:21  尔冬橙  阅读(453)  评论(0编辑  收藏  举报