一聚教程网:一个值得你收藏的教程网站

flex中Away3D 物体的拖拽移动(Mesh上按住鼠标拖动改对象)

时间:2015-12-05 00:00:00 编辑:简简单单 来源:转载

使用 Drag3D 可以很方便的实现对象的拖拽,下面通过样例进行演示。

 
实现功能:
(1)在舞台背景上按住鼠标拖拽,改变摄像头角度。
(2)在方块上按住鼠标拖拽,则是移动方块位置。
(3)默认方块在XY面上移动,按下键盘1、2、3键,移动面对应改称XY、XZ、ZY上移动。
(4)为方便定位,在舞台上添加坐标轴Trident
 
效果图如下:
原文:Away3D - 物体的拖拽移动(Mesh上按住鼠标拖动改对象)


代码如下

 代码如下

package{
    import flash.display.Sprite;
    import flash.display.StageAlign;
    import flash.display.StageScaleMode;
    import flash.events.Event;
    import flash.events.KeyboardEvent;
    import flash.events.MouseEvent;
    import flash.ui.Keyboard;
     
    import away3d.containers.View3D;
    import away3d.controllers.HoverController;
    import away3d.debug.Trident;
    import away3d.entities.Mesh;
    import away3d.events.MouseEvent3D;
    import away3d.materials.TextureMaterial;
    import away3d.primitives.CubeGeometry;
    import away3d.tools.utils.Drag3D;
    import away3d.utils.Cast;
     
    [SWF(frameRate="60", backgroundColor="#FFFFFF")]
    public class S6 extends Sprite {
         
        private  var _view3D:View3D;
        private var cameraController:HoverController;//360全景展示相机控制器
         
        [Embed(source="assets/cubeTexture3.jpg")]
        private var cubeTextureClass : Class;
         
        private var cubeTextureMaterial:TextureMaterial;
         
        private var _drag:Drag3D;
         
        //记录当前是移动摄像机还是移动物体
        private var _moveCamera:Boolean = true;
         
        private var _lastX:Number = 0;
        private var _lastY:Number = 0;
        private var _scale:Number = 1000;
         
        public function S6() {
            initEngine();
            initMaterials();
            initObjects();
            initListeners();
        }
         
        /**
         * 初始化引擎
         */
        private function initEngine():void
        {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            stage.align = StageAlign.TOP_LEFT;
             
            // 创建一个视口
            _view3D = new View3D();
            _view3D.antiAlias = 4; //设置抗锯齿级别
             
            //初始化摄像头
            cameraController = new HoverController(_view3D.camera);
            cameraController.tiltAngle = 15;
             
            addChild(_view3D);
        }
         
        /**
         * 初始化材质
         */
        private function initMaterials():void
        {
            cubeTextureMaterial = new TextureMaterial(Cast.bitmapTexture(cubeTextureClass));
        }
         
        /**
         * 初始化物体
         */
        private function initObjects():void
        {
            //添加 坐标系
            var trident:Trident = new Trident(500);
            _view3D.scene.addChild(trident);
             
            // 在三维舞台中创建一个方块
            var cube1:Mesh = new Mesh(new CubeGeometry(200, 200, 200, 1, 1, 1, false),
                cubeTextureMaterial);
            _view3D.scene.addChild(cube1);
             
            //开启鼠标事件支持
            cube1.mouseEnabled = true;
            //添加 监听
            cube1.addEventListener(MouseEvent3D.MOUSE_OVER, cubeMouseOver);
            cube1.addEventListener(MouseEvent3D.MOUSE_OUT, cubeMouseOut);
            cube1.addEventListener(MouseEvent3D.MOUSE_DOWN, cubeMouseDown);
             
            //给方块添加拖拽
            _drag = new Drag3D(_view3D, cube1, Drag3D.PLANE_XY);
        }
         
        //鼠标按下
        private function cubeMouseDown(event:MouseEvent3D):void
        {
            _moveCamera = false;
        }
         
        //鼠标移入
        private function cubeMouseOver(event:MouseEvent3D):void{
            var mesh:Mesh = event.object as Mesh;
            mesh.showBounds = true;
        }
         
        //鼠标移出
        private function cubeMouseOut(event:MouseEvent3D):void{
            var mesh:Mesh = event.object as Mesh;
            mesh.showBounds = false;
        }
         
        /**
         * 初始化监听
         */
        private function initListeners():void
        {
            addEventListener(Event.ENTER_FRAME, _onEnterFrame);
            //鼠标事件监听
            stage.addEventListener(MouseEvent.MOUSE_DOWN, onMouseDown);
            stage.addEventListener(MouseEvent.MOUSE_UP, onMouseUp);
            stage.addEventListener(MouseEvent.MOUSE_WHEEL,onWheel);
            stage.addEventListener(Event.RESIZE, onResize);
            stage.addEventListener(KeyboardEvent.KEY_UP, keyUpHandler);
            onResize();
        }
         
        //键盘响应事件,控制方块在某个平面区域移动
        private function keyUpHandler(event:KeyboardEvent):void
        {
            switch(event.keyCode)
            {
                case Keyboard.NUMBER_1:
                    _drag.plane = Drag3D.PLANE_XY;
                    break;
                case Keyboard.NUMBER_2:
                    _drag.plane = Drag3D.PLANE_XZ;
                    break;
                case Keyboard.NUMBER_3:
                    _drag.plane = Drag3D.PLANE_ZY;
                    break;
            }
        }
         
        /**
         * 渲染视图
         */
        private function _onEnterFrame(e:Event):void
        {      
            //渲染视图
            _view3D.render();
        }
         
        /**
         * 使用舞台大小一直全屏
         */
        private function onResize(event:Event = null):void
        {
            _view3D.width = stage.stageWidth;
            _view3D.height = stage.stageHeight;
        }
         
        /**
         * 鼠标滚轮事件
         */
        private function onWheel(event:MouseEvent):void
        {
            if (_moveCamera)
            {
                var dist:Number = cameraController.distance / 25;
                if (event.delta > 0)
                {
                    _scale -= dist;
                }
                else
                {
                    _scale += dist;
                }
                if (_scale > 40000)
                {
                    _scale = 40000;
                }
                if (_scale < 100)
                {
                    _scale = 100;
                }
                cameraController.distance = _scale;
            }
        }
         
        /**
         * 鼠标按下事件
         */
        private function onMouseDown(event:MouseEvent):void
        {
            if (_moveCamera)
            {
                _view3D.stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
                _lastX = _view3D.mouseX;
                _lastY = _view3D.mouseY;
            }
        }
         
        /**
         * 鼠标弹起事件
         */
        private function onMouseUp(event:MouseEvent):void
        {
            _view3D.stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler);
            _moveCamera = true;
        }
         
        /**
         * 鼠标移动事件
         */
        private function mouseMoveHandler(event:MouseEvent):void
        {
            if (_moveCamera)
            {
                //移动摄像机
                var dx:Number = _view3D.mouseX - _lastX;
                var dy:Number = _view3D.mouseY - _lastY;
                 
                cameraController.panAngle += dx;
                cameraController.tiltAngle += dy;
                 
                _lastX = _view3D.mouseX;
                _lastY = _view3D.mouseY;
            }
            else
            {
                //移动 3D 小球
                _drag.updateDrag();
            }
        }      
    }
}

文章评论

热门栏目