在TIleList的itemRenderer中的Flex 4.6控制计时器

时间:2013-08-27 05:34:16

标签: actionscript-3 flex timer air flex4

我正在使用Flex 4.6 AIR应用程序。我有一个磁贴列表,并且有一个图像作为itemRenderer。当我搜索图像时,数据不被过滤但是被选择了tilelist的索引(选定项),我在arrayCollection(dataProvider)中设置了一个自定义属性,然后调用了updateDisplayList,我在这个函数中启动了一个计时器。其中执行播放所选项目中的内部图像。

现在的问题是,如果我搜索该项目并单击未选择的项目,则不会停止计时器。我怎样才能做到这一点。

itemRenderer的代码在

之下
<?xml version="1.0" encoding="utf-8"?>
<s:ItemRenderer xmlns:fx="http://ns.adobe.com/mxml/2009"
                xmlns:s="library://ns.adobe.com/flex/spark"
                xmlns:mx="library://ns.adobe.com/flex/mx"
                autoDrawBackground="true" buttonMode="true"
                click="itemrenderer2_clickHandler(event)"
                doubleClick="itemrenderer1_doubleClickHandler(event)" doubleClickEnabled="true"
                rightClick="itemrenderer1_rightClickHandler(event)"
                rollOut="itemrenderer1_rollOutHandler(event)"
                rollOver="itemrenderer1_rollOverHandler(event)" useHandCursor="true">

    <fx:Script>
        <![CDATA[
            import customs.customcomponents.LibraryReDownloadComponent;
            import customs.customcomponents.VideoUploadBox;

            import dbconnection.Connection;

            import globalData.DataModel;

            import mx.collections.ArrayCollection;
            import mx.core.FlexGlobals;
            import mx.core.UIComponent;
            import mx.events.ItemClickEvent;
            import mx.managers.PopUpManager;
            import mx.utils.ObjectProxy;
            import mx.utils.UIDUtil;    

            private var isLoaded:Boolean = false;
            private var timer:Timer;
            private var rollOverFlag:Boolean = false;           
            private var countThumb:int = 0;
            private var arrThumnail:ArrayCollection;
            private var loaderThumb:Loader;     
            private var request:URLRequest;
            private var requestThumb:URLRequest;            
            private var loader:Loader;
            private var sqlStatStatus:SQLStatement;
            private var downloadVideoPath:String = "";
            private var videoUrlStream:URLStream;
            private var videoFileStream:FileStream;
            private var uploadvidbox:VideoUploadBox = new VideoUploadBox();
            private var dtStartVideo:Date;
            private var videoFileName:String = "";
            private var videoFile:File;
            private var IsRollOver:Boolean = false;                     

            [Bindable]
            private var modellocator:DataModel=DataModel.getInstance();

            private var connection:Connection = Connection.getInstance();

            override public function set data(value:Object):void
            {
                if(value != null)
                {
                    super.data = value;

                    if(timer != null)
                    {
                        timer.stop();
                        timer.removeEventListener(TimerEvent.TIMER, showFrame); 
                        timer = null;
                    }

                    loaderThumb = new Loader();                 
                    rollOverFlag = false;                   
                    countThumb = 0; 

                    if(flash.system.Capabilities.os.indexOf("Mac") > -1)
                    {
                        requestThumb = new URLRequest("file://" + data.Videothumbnail);
                    }
                    else
                    {
                        requestThumb = new URLRequest(data.Videothumbnail);
                    }

                    loaderThumb.contentLoaderInfo.addEventListener(Event.COMPLETE,onThumbComplete);                 
                    loaderThumb.load(requestThumb); 

                    if(data.VideoBuyFlag == "yes")
                    {
                        if(data.VideoIsDeleted == "No")
                        {
                            bContain.setStyle("borderColor", "#00FF18");
                            bContain.alpha = 1;
                        }
                        else
                        {
                            bContain.setStyle("borderColor", "#888888");
                            bContain.alpha = 0.3;
                        }                                               
                    }
                    else
                    {
                        if(data.VideoIsDeleted == "No")
                        {
                            bContain.setStyle("borderColor", "#C50000");
                            bContain.alpha = 1;
                        }
                        else
                        {
                            bContain.setStyle("borderColor", "#888888");
                            bContain.alpha = 0.3;
                        }                                           
                    }

                    if(data.VideoStatus == "Active")
                    {
                        imgActive.source = "assets/btn_Active.jpg";
                        bContain.alpha = 1;
                    }
                    else
                    {
                        imgActive.source = "assets/btn_InActive.jpg";
                        bContain.alpha = 0.3;
                    }

                    arrThumnail = new ArrayCollection();

                    if(data.Videothumbdata.thumbimage != null)
                    {
                        if(data.Videothumbdata.thumbimage.source.item.length > 1)
                        {
                            for(var i:int = 0; i < data.Videothumbdata.thumbimage.source.item.length; i++)
                            {
                                arrThumnail.addItem(data.Videothumbdata.thumbimage.source.item[i]);
                            }
                        }
                        else
                        {
                            arrThumnail.addItem(data.Videothumbdata.thumbimage.source.item);                            
                        }
                    }                   
                }
            }           

            override protected function updateDisplayList(unscaledWidth:Number, unscaledHeight:Number):void
            {
                super.updateDisplayList(unscaledWidth, unscaledHeight);             
                /* trace(data.Videoname);
                trace("IsRollOver : " + IsRollOver.toString());
                trace("IsSearching : " + data.IsSearching.toString());
                trace("rollOverFlag : " + rollOverFlag.toString());  */         

                if(data.IsSearching == false)
                {
                    if(IsRollOver == false)
                    {                       
                        if(timer != null)
                        {
                            trace("inner");
                            loaderThumb = new Loader();
                            rollOverFlag = false;                           

                            if(flash.system.Capabilities.os.indexOf("Mac") > -1)
                            {
                                requestThumb = new URLRequest("file://" + data.Videothumbnail);
                            }
                            else
                            {
                                requestThumb = new URLRequest(data.Videothumbnail);
                            }

                            loaderThumb.contentLoaderInfo.addEventListener(Event.COMPLETE,onThumbComplete);                 
                            loaderThumb.load(requestThumb);

                            timer.stop();
                            timer.removeEventListener(TimerEvent.TIMER, showFrame);                         
                            countThumb= 0;  
                            System.gc();
                            System.gc();
                        }                       
                    }
                }
                else
                {                   
                    if(rollOverFlag == false)
                    {
                        rollOverFlag = true;                        
                        trace("Hi");
                        timer = new Timer(1000);
                        timer.addEventListener(TimerEvent.TIMER, showFrame);
                        timer.start();
                    }
                    else
                    {
                        if(timer != null)
                        {
                            trace("SecondInner");
                            loaderThumb = new Loader();
                            rollOverFlag = false;                           

                            if(flash.system.Capabilities.os.indexOf("Mac") > -1)
                            {
                                requestThumb = new URLRequest("file://" + data.Videothumbnail);
                            }
                            else
                            {
                                requestThumb = new URLRequest(data.Videothumbnail);
                            }

                            loaderThumb.contentLoaderInfo.addEventListener(Event.COMPLETE,onThumbComplete);                 
                            loaderThumb.load(requestThumb);

                            timer.stop();
                            timer.removeEventListener(TimerEvent.TIMER, showFrame);                         
                            countThumb= 0;  
                            System.gc();
                            System.gc();
                        }                       
                    }  
                }               
            }                   

            protected function showFrame(event:TimerEvent):void
            {
                var file:File;
                trace("hello");
                if(countThumb < arrThumnail.length)
                {
                    if(flash.system.Capabilities.os.indexOf("Mac") > -1)
                    {
                        file = new File("file://" + arrThumnail[countThumb]);
                    }
                    else
                    {
                        file = new File(arrThumnail[countThumb]);
                    }

                    if(file.exists)
                    {
                        loader = new Loader();

                        if(flash.system.Capabilities.os.indexOf("Mac") > -1)
                        {
                            request=new URLRequest("file://" + arrThumnail[countThumb]);
                        }
                        else
                        {
                            request=new URLRequest(arrThumnail[countThumb]);
                        }

                        loader.contentLoaderInfo.addEventListener(Event.COMPLETE,onComplete);
                        loader.load(request);

                        if(countThumb == (arrThumnail.length - 1))
                        {                   
                            countThumb = 0;
                        }
                        else
                        {
                            countThumb++;                   
                        }
                    }
                    else
                    {
                        countThumb++;
                    }
                }
                else
                {
                    countThumb = 0;
                }                               
            }

            protected function itemrenderer1_rollOverHandler(event:MouseEvent):void
            {           
                if(data.IsSearching == false)
                {
                    rollOverFlag = true;
                    IsRollOver = true;

                    if(arrThumnail != null && arrThumnail.length > 0)
                    {
                        timer = new Timer(1000);
                        timer.addEventListener(TimerEvent.TIMER, showFrame);
                        timer.start();                                  
                    }
                }               
            }

            private function onComplete(event:Event):void 
            {   
                if(rollOverFlag)
                {
                    imgThumb.source = loader;
                }               
            }

            private function onThumbComplete(event:Event):void
            {
                imgThumb.source = loaderThumb;              
            }

            protected function itemrenderer1_rollOutHandler(event:MouseEvent):void
            {           
                if(data.IsSearching == false)
                {
                    rollOverFlag = false;
                    IsRollOver = false;                 
                    imgThumb.source = loaderThumb;

                    if(timer != null)
                    {
                        timer.stop();
                        timer.removeEventListener(TimerEvent.TIMER, showFrame); 
                        timer = null;
                    }

                    countThumb = 0; 
                    System.gc();
                    System.gc();
                }               
            }       

            protected function itemrenderer2_clickHandler(event:MouseEvent):void
            {               
                for(var i:int=0; i < modellocator.libraryvideoac.length; i++)
                {                                       
                    modellocator.libraryvideoac[i].IsSearching = false;             
                }               

                parentDocument.parentDocument.txt_search.text = resourceManager.getString('languages','lblSearchText');             
                parentDocument.parentDocument.unCheckSelection();

                if(data.VideoIsDeleted == "Yes")
                {
                    var popupReDownload:LibraryReDownloadComponent = new LibraryReDownloadComponent();
                    popupReDownload = PopUpManager.createPopUp(UIComponent(this.parentApplication), LibraryReDownloadComponent, true) as LibraryReDownloadComponent;
                    popupReDownload.addEventListener("downloadMovie", redownloadMovie);
                    PopUpManager.centerPopUp(popupReDownload);
                }               
            }   

        ]]>
    </fx:Script>

    <fx:Declarations>
        <mx:NumberFormatter id="numFormat" precision="2"/>
    </fx:Declarations>

    <s:BorderContainer id="bContain" left="2" top="2" width="92" height="67" backgroundAlpha="1"
                       backgroundColor="#000000" borderColor="#030304" borderWeight="3">            
        <s:Image id="imgThumb" width="86" height="61" fillMode="scale" scaleMode="stretch"/>
        <s:Image id="imgActive" right="0" bottom="0" width="15" height="15" buttonMode="true"
                 useHandCursor="true"/>     
    </s:BorderContainer>    
</s:ItemRenderer>

1 个答案:

答案 0 :(得分:0)

您在代码中的不同位置进行了计时器实例化。我不能确定实际的程序流程,但也许您实例化计时器两次?在这种情况下,程序将覆盖定时器变量,并失去对第一个定时器的引用。所以,当你调用timer.stop()时,你实际上只停止一个计时器,而另一个计时器在后台继续运行。

您没有在此处设置弱引用属性:

timer.addEventListener(TimerEvent.TIMER, showFrame);

因此,如果您丢失其引用(即使您调用System.gc()两次:-)),也不会对计时器进行垃圾回收。检查一下这种情况,然后尝试添加一个监听器:

timer.addEventListener(TimerEvent.TIMER, showFrame, false, 0, true);