`
hanjiangduzhuo
  • 浏览: 83398 次
  • 性别: Icon_minigender_1
  • 来自: 长沙
最近访客 更多访客>>
社区版块
存档分类
最新评论

Flex切换播放

    博客分类:
  • Flex
阅读更多
  1. <?xml version="1.0" encoding="utf-8"?>   
  2.      <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute"    
  3.     
  4.  backgroundColor="white" width="300" height="200"  creationComplete="getResult()">   
  5.          <mx:Style>    
  6.          .vbox {   
  7.              backgroundColor: #000000;   
  8.              textAlign:center;   
  9.              borderStyle:solid;   
  10.              borderColor:#ffffff;   
  11.          }    
  12.          </mx:Style>    
  13.   <mx:Script>   
  14.    <![CDATA[   
  15.     import mx.containers.VBox;   
  16.     import flash.utils.setInterval;   
  17.     import mx.containers.Canvas;   
  18.     import mx.controls.Label;   
  19.     import mx.controls.Button;   
  20.     import mx.controls.Text;   
  21.     import mx.controls.Alert;       
  22.     import mx.rpc.events.*;   
  23.     import mx.collections.*;   
  24.     import mx.controls.*;   
  25.        
  26.     import flash.utils.*;   
  27.     import flash.events.TimerEvent;   
  28.        
  29.        import flash.events.ContextMenuEvent;   
  30.           
  31.     private var newsListXml:XML;   
  32.     private var newsList:XMLListCollection;   
  33.     private var PicUrl:String = "E:\\ad\\";   
  34.     private var newsArray:Array = new Array; //全局变量,用来装vbox集合   
  35.     private var currNews:News;   
  36.     private var newsArrayLength:int;   
  37.       
  38.     //发送请求去读取xml文件信息   
  39.     public function getResult():void{   
  40.        feed.url="http://127.0.0.1:8080/webroot/file/bigAds.xml";   //xml文件路径   
  41.        feed.send(null);         
  42.     }   
  43.     //请求xml成功后处理函数   
  44.     public function resultHandler(event:ResultEvent):void {   
  45.      canvasid.setStyle("backgroundImage",PicUrl+"5.jpg");   
  46.      title.text="哈1";   
  47.      newsListXml = XML(event.result);   
  48.      init();   
  49.      autoPlay();   
  50.     }   
  51.        
  52.     public function titileStyle():void{   
  53.      var glowFilter:GlowFilter=new GlowFilter();   
  54.      glowFilter.color=0xFFFFFF;   
  55.      var sampleFilters:Array = title.filters;   
  56.      sampleFilters.push(glowFilter);   
  57.      title.filters = sampleFilters;       
  58.     }   
  59.        
  60.     //错误时处理的函数   
  61.     public function handleFault(event:FaultEvent):void{   
  62.      Alert.show(event.fault.faultString, "Error");       
  63.     }   
  64.           
  65.     //动态创建标签   
  66.     public function init():void{        
  67.         newsList = new XMLListCollection(newsListXml.children().child(1).children());   
  68.         var lb:Label;   
  69.         var vbox:VBox;   
  70.         for(var i:int=0;i<newsList.length;i++){           
  71.          var newsObject:Object=newsList.getItemAt(i);           
  72.           lb = new Label();   
  73.           var picId:String=newsObject.@pk;   
  74.           lb.id = "pic"+picId;    //存id   
  75.           lb.text = String(i+1);       
  76.           var picName:String=newsObject.@pk+".jpg";   //存图片路径          
  77.           lb.name = picName;   //存图片名字   
  78.           lb.setStyle("color","#ffffff");   
  79.           lb.useHandCursor = true;   
  80.           lb.buttonMode = true;   
  81.           lb.mouseChildren = false;   
  82.           lb.height=15;   
  83.           lb.width=22;   
  84.           var title:String =newsObject.entry.(@key=="title");   
  85.             
  86.              vbox = new VBox();   
  87.           vbox.id = ""+i;   
  88.              
  89.           vbox.addChild(lb);   
  90.                   
  91.           vbox.styleName="vbox";   
  92.           addIndexEvent(lb,vbox);     //动态为tx标签元素添加事件     
  93.            HboxIndexSet.addChild(vbox);   
  94.               
  95.            var news:News=new News();   
  96.            news.seq=i;   
  97.            news.id=newsObject.@pk;   
  98.            news.title=newsObject.entry.(@key=="title");   
  99.            news.img=newsObject.@pk+".jpg";   
  100.            news.indexBox=vbox;             
  101.            newsArray[i]=news;   
  102.        }   
  103.           
  104.        titileStyle();         
  105.        newsArrayLength=newsArray.length;   
  106.        currNews=newsArray[newsArrayLength-1];         
  107.        setInterval(function():void{autoPlay();},3000);                           //as2.0中用setInterval   
  108.        ///var ttime:Timer = new Timer(2000,0);   
  109.       /// ttime.addEventListener(TimerEvent.TIMER,function():void{autoPlay();});   //as3.0中用time取代了setInterval   
  110.        ///ttime.start();         
  111.     }   
  112.        
  113.     //动态为标签添加事件   
  114.        public function addIndexEvent(tagetObject:Object,box:VBox):void{   //参数一代表哪个标签对像,二是一个变量参数   
  115.      tagetObject.addEventListener(MouseEvent.CLICK,function():void{clickIndex(box);});   
  116.        }   
  117.           
  118.           //处理事件函数   
  119.     public function clickIndex(box:VBox):void{   
  120.      toTop(newsArray[box.id])   
  121.     }   
  122.        
  123.     //自动切换(播放)   
  124.     public function autoPlay():void{   
  125.      var currSeq:int=currNews.seq;   
  126.      var nextSeq:int=(currSeq+1)%newsArrayLength;   
  127.      var nextNews:News=newsArray[nextSeq];   
  128.      toTop(nextNews);   
  129.     }   
  130.        
  131.     //图片切换,背景色切换   
  132.     public function toTop(nextNews:News):void{   
  133.      currNews.indexBox.setStyle("backgroundColor","#000000");   
  134.      nextNews.indexBox.setStyle("backgroundColor","red");   
  135.      canvasid.setStyle("backgroundImage",PicUrl+nextNews.img);       
  136.      title.text=nextNews.title;   
  137.      currNews=nextNews;      
  138.          
  139.     }   
  140.         
  141.        
  142.     //打开一个浏览器(暂没用)   
  143.  //   public function showPic():void{   
  144.  //    navigateToURL(new URLRequest("http://www.baidu.com"),"_blank");       
  145.  //   }   
  146.    ]]>   
  147.   </mx:Script>   
  148.      
  149.      
  150.  <mx:HTTPService id="feed" resultFormat="e4x" result="resultHandler(event)"    
  151.  fault="handleFault(event);" useProxy="false" />   
  152.  <mx:VBox width="300" height="200" verticalAlign="bottom" verticalGap="0"     
  153.  id="canvasid" horizontalCenter="0" verticalCenter="0" verticalScrollPolicy="off"  
  154.     
  155.  horizontalScrollPolicy="off">   
  156.         <mx:Label x="123" y="180" text="Label" height="26"    
  157.     
  158.  color="#000000" fontSize="20" fontWeight="bold" id="title"   />   
  159.         <mx:HBox x="211" y="180" width="100%"    
  160.     
  161.  backgroundColor="#000000" backgroundAlpha="0.50"   horizontalAlign="right" id="HboxIndexSet"    
  162.     
  163.  horizontalGap="1" horizontalScrollPolicy="off"/>   
  164.       </mx:VBox>   
  165.       
  166.     
  167.   </mx:Application>   
  168.   
分享到:
评论
1 楼 haolixia1982 2009-05-21  
老大,那个News是个什么东东
?能不能提供以下?

相关推荐

Global site tag (gtag.js) - Google Analytics