工具类:给文本添加滚动

语言:LayaBox - AS3
工具类作用:自动给文本添加滚动,当文本显示不下时,添加这个脚本,就可以滚动看到剩下的内容了

工具类代码:

package UIEngine {
    import laya.components.Script;
    import laya.events.Event;
    import laya.display.Text;
    
    public class ScrollText extends Script {
        /** @prop {name:scrollType, tips:"滚动方向,1水平2垂直", type:Int, default:1}*/
        public var scrollType: int = 1;
        private var prevX:Number = 0;//上一次的鼠标x坐标
        private var prevY:Number = 0;//上一次的鼠标y坐标
        private var isAutoMove:Boolean = false;//自动滚动一次的标志
        private var maxScrollX:Number = 0;//水平最大滚动距离
        private var maxScrollY:Number = 0;//垂直最大滚动距离
        private var waitTotalTime:Number = 1000;//打开界面后等待多久之后自动滚动
        private var waitcountTime:Number = 0;//统计打开界面之后经过的时间

        private var _ownText:*;
    

        override public function onEnable():void {
            _ownText = this.owner;
            _ownText.overflow = Text.SCROLL;
            _ownText.on(Event.MOUSE_DOWN,this,startScrollText)
            maxScrollX = _ownText.maxScrollX;
            maxScrollY = _ownText.maxScrollY;
            waitcountTime = 0;
            isAutoMove = true;
        }

        public function startScrollText(e:Event):void
        {
            prevX = _ownText.mouseX;
            prevY = _ownText.mouseY;
            Laya.stage.on(Event.MOUSE_UP,this,finishScrollText)
            Laya.stage.on(Event.MOUSE_MOVE,this,scrollText)
        }

        public function finishScrollText(e:Event):void
        {
            Laya.stage.off(Event.MOUSE_UP,this,finishScrollText)
            Laya.stage.off(Event.MOUSE_MOVE,this,scrollText)
        }

        public function scrollText(e:Event):void
        {
            var nowX:Number = _ownText.mouseX;
            var nowY:Number = _ownText.mouseY;
            
            if(scrollType == 1){
                _ownText.scrollX += prevX - nowX;
            }else if(scrollType == 2){
                _ownText.scrollY += prevY - nowY;
            }
            prevX = nowX;
            prevY = nowY;
        }

        override public function onDisable():void {
        }

        override public function onLateUpdate():void{
            if(isAutoMove){
                if(waitcountTime

调用工具类:

private var _memberName:Label;
···
_memberName.addComponent(ScrollText);

你可能感兴趣的:(工具类:给文本添加滚动)