as3实现文本滚动多适应解决方案

作者:袖梨 2022-06-28
 代码如下 复制代码


package com
{

        import flash.display.Sprite;
        import flash.display.MovieClip;

        import flash.events.Event;
        import flash.events.MouseEvent;
        import flash.text.TextField;
        import flash.geom.Rectangle;

        public class detailsBoxScrolling extends Sprite
        {
                private var __detailsBox:MovieClip;
                private var __notVisibleHeight:Number;
                private var __sliderHeight:Number;
                private var __sliderY:Number;
                private var __scrollStep:Number = 1.80000E-001;

                public function detailsBoxScrolling ()
                {
                        init ();
                }

                public function init ()
                {
                        __detailsBox = new detailsBox  ;
                        __detailsBox.scrollBar.scrollBox.mask = __detailsBox.scrollBar.scrollMasks;
                        __detailsBox.x = 60;
                        __detailsBox.y = 60;

                        //不可见区域高度
                        __notVisibleHeight = __detailsBox.scrollBar.scrollBox.height - __detailsBox.scrollBar.bg.height;

                        //滑块滚动高度
                        __sliderHeight = __detailsBox.scrollBar.bg.height - __detailsBox.scrollBar.slider.height;

                        addChild (__detailsBox);

                        //滑块事件
                       
                        //问题:不知道为什么会这样,为什么黑色块和红色块会被分离,这个有点不明白,
                        //难道鼠标事件的这个元件“__detailsBox.scrollBar.slider”不包含里面的MC元件吗?
                        //迫切求解?(见1)
                        __detailsBox.scrollBar.slider.buttonMode=true;
                        //1 (你的slider有嵌套元件,所以禁止子元件接收事件)
                        __detailsBox.scrollBar.slider.mouseChildren=false;
                        __detailsBox.scrollBar.slider.addEventListener (MouseEvent.MOUSE_DOWN,sliderDownFunc);
                }

                //更新内容函数
                private function updateContentFunc ()
                {
                        __detailsBox.scrollBar.scrollBox.addEventListener (Event.ENTER_FRAME,bufferFunc);

                }

                //缓冲区函数
                private function bufferFunc (e:Event)
                {
                        e.target.y = Math.floor(e.target.y-__scrollStep*((__sliderY * (__notVisibleHeight / __sliderHeight))+e.target.y));
                }

                //滑块
                //问题:void这个叫函数返回值,可是我并不明白到底怎么样才返回(想直接通过运行函数得到某个运行后的结果,就可以用返回值)
               
                //而我在这里加上去感觉并没有多大的意义似的。在什么情况他才返回呢?(见2 3);
                //求解!
                private function sliderDownFunc (e:Event):void
                {
                        var rec:Rectangle = new Rectangle(0,0,0,__sliderHeight);
                       
                        e.target.startDrag (false,rec);
                        //2  鼠标滑块移动(这个也一定要侦听stage的)
                        stage.addEventListener (MouseEvent.MOUSE_MOVE,sliderMoveFunc);
                        //3 鼠标滑块松开(松开一定要侦听stage的)
                        stage.addEventListener (MouseEvent.MOUSE_UP,sliderUpFunc);
                       

                }

                private function sliderMoveFunc (e:Event):void
                {
                        //问题:此处需要鼠标绝对移到滑块上才会执行,
                        //不知道有没有其他方法能使鼠标按紧后不松开并超出滑块范围也能执行?
                        //求解!
                        __sliderY = __detailsBox.scrollBar.slider.y;
                        updateContentFunc ();
                }

                private function sliderUpFunc (e:Event):void
                {
                       
                        stopDrag ();
                        stage.removeEventListener (MouseEvent.MOUSE_MOVE,sliderMoveFunc);
                        stage.removeEventListener (MouseEvent.MOUSE_MOVE,sliderMoveFunc);
                }

        }
}

相关文章

精彩推荐