最近作一個決策分析項目,須要使用到FLEX,而在使用的過程當中,逐漸的接觸到了FLASH,忽然發現,用FLASH來作動畫或者圖表,用FLEX與JAVA後臺進行交互,是一個很是方便的事情。惟一的障礙莫過於,FLEX與FLASH之間的數據交互。數組
其實這自己也不難,解決的方法就是經過Actionscript3.0.這須要注意的是,Flash cs5以上的版本才支持AS3.0,更之前的版本只支持AS2.0,我用的FlexBuilder是4.6,一樣支持AS3.0。這樣促使了As3.0能夠完美的成爲中間橋樑app
如下代碼爲FLEX端測試
<?xml version="1.0" encoding="utf-8"?> <s:Application xmlns:fx=" http://ns.adobe.com/mxml/2009 " xmlns:s="library://ns.adobe.com/flex/spark" xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600" creationComplete="application1_creationCompleteHandler(event)"> <fx:Script> <![CDATA[ import mx.events.FlexEvent; private var flexArr:Array = new Array(); protected function swfLoader_completeHandler(event:Event):void { // flash加載完成後,傳入參數 Object(swfLoader.content).InitApp(this,this.flexArr,"交互測試"); } protected function application1_creationCompleteHandler(event:FlexEvent):void { this.flexArr.push(1111,2222,3333,4444); swfLoader.source="flash/RecComp.swf"; } ]]> </fx:Script> <s:SWFLoader source="" id="swfLoader" width="190" height="373" left="100" top="100" complete="swfLoader_completeHandler(event)" /> </s:Application>
從上面代碼能夠看出,FLEX調用了一個RecComp.swf的Flash文件,在這個swf中,要寫一個InitApp方法,傳入一個對象(供FLASH調用FLEX,後續講)一個數組用來存值和一個標題,以下flex
import flash.display.MovieClip; import flash.display.Sprite; import flash.text.TextField; stop(); var flexApp:Object; function InitApp(ap:Object,aarFlex:Array,titleMsg:String):void { this.flexApp = ap; var array:Array = new Array(); var rec1:Rec1 = new Rec1(); var rec2:Rec2 = new Rec2(); var rec3:Rec3 = new Rec3(); var rec4:Rec4 = new Rec4(); var rec5:Rec5 = new Rec5(); var rec6:Rec6 = new Rec6(); var rec7:Rec7 = new Rec7(); var rec8:Rec8 = new Rec8(); array.push(rec1,rec2,rec3,rec4,rec5,rec6,rec7,rec8); var format:TextFormat = new TextFormat("微軟雅黑",22,"0x663300"); format.bold="bold"; format.align = "center"; var tf:TextField=new TextField(); tf.height=30; tf.width=100; tf.x=12.15; tf.y=47.3; tf.text=titleMsg; tf.setTextFormat(format); addChild(tf); for(var i:int=0;i<aarFlex.length;i++){ array[i].x=15; array[i].y=300 - 35*i; var recInfo:RecInfo=new RecInfo(); recInfo.x=155; recInfo.y=300 - 35*i - 3; recInfo.titleInfo.text=aarFlex[i]+"人"; addChild(array[i]); addChild(recInfo); setChildIndex(array[i],i+1); } }
function InitApp(ap:Object,aarFlex:Array,titleMsg:String):void 這個方法爲主要接收參數。至於裏面別的代碼,是我本身作的無件,與交互不相關。這個組件我會發至網盤,裏面還包含了經過Flash來作動畫,作元件等、供你們交流動畫