首页 > 代码库 > [ActionScript 3.0] 幻灯片效果实例

[ActionScript 3.0] 幻灯片效果实例

package com.fylibs.components.effects
{
    import com.fylibs.utils.LoaderQueues;
    import com.tweener.transitions.Tweener;
    import com.views.LoadingView;
    
    import flash.display.Bitmap;
    import flash.display.Sprite;
    import flash.utils.clearTimeout;
    import flash.utils.setTimeout;
    
    /**
     * @author: Frost.Yen
     * @E-mail: 871979853@qq.com
     * @create: 2016-4-4 下午10:47:20
     * 幻灯片播放
     */
    public class SlideShow extends Sprite
    {
        private var _container:Sprite;
        private var _slideWidth:Number;
        private var _slideHeight:Number;
        private var _timeoutFade:uint;//控制切换图片的setTimeout
        private var _intervalFade:Number = 1000 * 5;//5s切换一次
        private var _duration:Number = 3;//切换过渡时长
        private var _dotBar:Sprite;
        private var _hasDot:Boolean;
        private var _page:int;
        private var _loadingView:LoadingView;
        public function SlideShow(w:Number,h:Number,hasDot:Boolean=false)
        {
            _slideWidth = w;
            _slideHeight = h;
            _hasDot = hasDot;
            initViews();
        }
        private function initViews():void
        {
            _dotBar = new Sprite();
            _container = new Sprite();
            _loadingView = new LoadingView();
            this.addChild(_container);
            this.addChild(_dotBar);
            
        }
        public function setSlide(url:Array):void
        {
            _page = url.length;
            _loadingView.addLoading(this,1920,770,0,74);
            for(var i:int=0;i<_page;i++){
                LoaderQueues.getInstance().addRes(url[i],onSlideLoaded);
            }
            if(_hasDot){
                initDot();
            }
        }
    
        private function onSlideLoaded(bmp:Bitmap):void
        {
            var sp:Sprite = new Sprite();
            bmp.width = _slideWidth;
            bmp.height = _slideHeight;
            sp.addChild(bmp);
            sp.name = "sp_"+_container.numChildren;
            _container.addChildAt(sp,0);
            if(_container.numChildren == _page){
                _loadingView.removeLoading();
                control(_container.numChildren - 1);
            }
        }
        /**
         * 控制播放内容的渐变轮换
         */
        private function fadeControl():void
        {
            if (_container.numChildren > 1) {
                setDotState(int(_container.getChildAt(_container.numChildren - 2).name.split("_")[1]));
                Tweener.addTween(_container.getChildAt(_container.numChildren - 1), { alpha:0, time:_duration} );
                Tweener.addTween(_container.getChildAt(_container.numChildren - 2), { alpha:1, time:_duration ,onComplete:fadeComplete} );
            }
        }
        /**
         * 播放内容轮换渐变结束,设置内容的层次
         */
        private function fadeComplete():void
        {
            control(_container.numChildren - 2);
            
            _container.setChildIndex(_container.getChildAt(_container.numChildren - 1), 0);
            
        }
        /**
         * 根据索引去处理图片轮换
         * @param    id
         */ 
        private function control(id:int):void
        {
            var obj:Sprite = _container.getChildAt(id) as Sprite;
            _timeoutFade = setTimeout(fadeControl, _intervalFade);
        }
        /**
         * 初始化表示页面的小圆点
         */
        private function initDot():void
        {
            disposeDot();
            for(var i:int=0;i<_page;i++){
                var dot:Sprite = createDot();
                dot.x = i*(dot.width+8);
                if(i==0){
                    dot.alpha = 1;
                }else{
                    dot.alpha = 0.5;
                }
                _dotBar.addChild(dot);
            }
            _dotBar.x = (_slideWidth-_dotBar.width)*0.5;
            _dotBar.y = _slideHeight - 20;
        }
        /**
         * 创建表示页面的小圆点
         */
        private function createDot():Sprite
        {
            var dot:Sprite = new Sprite();
            dot.graphics.beginFill(0xffffff,1);
            dot.graphics.drawCircle(0,0,5);
            dot.graphics.endFill();
            return dot;
        }
        /**
         * 移除表示页面的小圆点
         */
        private function disposeDot():void
        {
            while(_dotBar.numChildren){
                (_dotBar.getChildAt(0) as Sprite).graphics.clear();
                _dotBar.removeChildAt(0);
            }
        }
        /**
         * 设置表示页码的小圆点状态
         * @param      id 小圆点的id
         */
        private function setDotState(id:int):void
        {
            for(var i:int = 0;i<_dotBar.numChildren;i++){
                if(id == i){
                    _dotBar.getChildAt(i).alpha = 1;
                }else{
                    _dotBar.getChildAt(i).alpha = 0.5;    
                }
            }
        }
        public function stop():void
        {
            clearTimeout(_timeoutFade);            
        }
        public function play():void
        {
            _timeoutFade = setTimeout(fadeControl, _intervalFade);
        }
    }
}

 

[ActionScript 3.0] 幻灯片效果实例