[ActionScript 3.0] AS3 用于拖动对象时跟随鼠标的缓动效果

package com.fylibs.components.effects
{
    import flash.display.DisplayObject;
    import flash.events.Event;
    import flash.events.MouseEvent;

    /**
     * 用于拖动对象时的缓动效果
     * @author Frost.Yen
     * @E-mail 871979853@qq.com
     * @create 2015-12-11 下午3:22:27
     *
     */
    public class DragMove
    {
        private var _direction:String = "horizontal";
        private var _ratio:Number = 0.900000;
        /**
         * 按下的坐标(当horizontal时为x坐标,当vertical时为y坐标)
         */
        private var _downCoor:Number;
        /**
         * 移动的坐标(当horizontal时为x坐标,当vertical时为y坐标)
         */
        private var _moveCoor:Number;
        /**
         * 缓动偏移量
         */
        private var _offset:Number;
        /**
         * 坐标标记(当horizontal时为x,当vertical时为y)
         */
        private var _coor:String = "mouseX";
        /**
         * 标记是否按下状态
         */
        private var _isDown:Boolean;
        private var _target:DisplayObject;
        private var _max:Number;
        private var _min:Number;
        /**
         * 拖拽缓动
         * @param target  需要拖拽的对象
         * @param max  拖拽对象的最大坐标值
         * @param min  拖拽对象的最小坐标值
         * @param direction  缓动方向
         */
        public function DragMove(target:DisplayObject,max:Number,min:Number,direction:String="horizontal")
        {
            _max = max;
            _min = min;
            _direction = direction;
            _direction == "horizontal"?_coor = "mouseX":_coor = "mouseY";
            _target = target;
            _target.addEventListener(MouseEvent.MOUSE_DOWN,onDown);
        }
        /**
         * 刷新
         * @param max  拖拽对象的最大坐标值
         * @param min  拖拽对象的最小坐标值
         */
        public function refresh(max:Number,min:Number):void
        {
            _max = max;
            _min = min;
        }
        private function onDown(e:MouseEvent):void
        {
            _isDown = true;
            _downCoor = _target[_coor];
            _target.stage.addEventListener(MouseEvent.MOUSE_MOVE, onMove);
            _target.stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
            _target.addEventListener(Event.ENTER_FRAME,onEnterFrame);
        }
        private function onEnterFrame(e:Event):void
        {
            _offset = _offset * _ratio;
            _target.x = _target.x + _offset;
            if (_target.x < _min){
                _target.x = _min ;
            } 
            if (_target.x > _max){
                _target.x = _max;
            }
            if(Math.abs(_offset)<0.001){
                _offset+=0.1;
                _target.removeEventListener(Event.ENTER_FRAME,onEnterFrame);
            }
        }
        private function onMove(e:MouseEvent):void
        {
            if (_isDown) {
                _moveCoor = _target[_coor];
                _offset = _moveCoor - _downCoor
                _offset = _offset / 20;
            }
        }
        private function onUp(e:MouseEvent):void
        {
            _isDown = false;
            _target.stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
            _target.stage.removeEventListener(MouseEvent.MOUSE_MOVE, onMove);

        }
        /**
         * 移动方向,horizontal横向,vertical纵向
         */
        public function get direction():String
        {
            return _direction;
        }

        /**
         * @private
         */
        public function set direction(value:String):void
        {
            _direction = value;
        }


        /**
         * 缓动系数
         */
        public function get ratio():Number
        {
            return _ratio;
        }

        /**
         * @private
         */
        public function set ratio(value:Number):void
        {
            _ratio = value;
        }


    }
}

 

posted on 2016-02-03 16:34  晏过留痕  阅读(1611)  评论(0编辑  收藏  举报