2010-12-10 52 views
0

我在Flash CS5/AS3項目中有一個內容滾動條,我想在多個位置使用這些項目,同時在這些不同位置的每個位置加載不同的內容(html)。Flash AS3編碼變量對每次調用的初始化方式不同

下面是滾動文本組件的AS3代碼。 'compnentsettings.xml'是我定義要調用什麼.html文件的地方。

現在我只有一個地方的滾輪工作。我可以克隆它,但當然,它會加載相同的數據。

我相信我有修改從scrolledTextComponent.as(下圖)settingsComponent函數加載不同compnentSettings.xml文件

我需要的變量初始化__xmlSettingsPath每次調用不同的,我不知道該怎麼辦這個。請幫忙!

package com 
{ 
    import flash.display.*; 
    import flash.events.TimerEvent; 
    import flash.net.URLRequest; 
    import flash.net.URLLoader; 
    import flash.text.StyleSheet; 
    import flash.utils.Timer; 
    import flash.events.MouseEvent; 
    import flash.text.TextField; 
    import flash.events.*; 

    import com.LoadXmlPlayerClass; 

    public class scrolledTextComponent extends MovieClip 
    { 

     // component variables 
     private var __xmlSettingsPath:String = "componetsettings.xml"; 
     private var _sheet:StyleSheet = new StyleSheet();  
     public var _loadSettingsXml:LoadXmlPlayerClass = new LoadXmlPlayerClass(); 
     private var _settingsXmlTimmer:Timer = new Timer(250);  

     public var _loadXml:LoadXmlPlayerClass = new LoadXmlPlayerClass(); 
     private var _newsTimmer:Timer = new Timer(250);  


     //private var _loaderIcon_mc:LoaderMC = new LoaderMC(); //used to show xml loading state 
     private var _urlCSS:URLRequest = new URLRequest(); 
     private var _loaderCSS = new URLLoader(); 

     private var _urlHTML:URLRequest = new URLRequest(); 
     private var _loaderHTML = new URLLoader(); 


     //text 
     private var _txt:TextField = new TextField(); 
     private var _MyScrollBar:MyScrollBar = new MyScrollBar(); 

     public function scrolledTextComponent() 
     { 
      loaderIcon_mc.visible=true; 
      readSettingsXml(); 

     } 



     /* Other Functions */ 
     private function readSettingsXml() :void 
     { 
      if(__xmlSettingsPath != '') 
      { 
       //loaderIcon_mc.visible=false; 
       //read XML file 
       _loadSettingsXml.loadFile(__xmlSettingsPath); 
       _settingsXmlTimmer.addEventListener(TimerEvent.TIMER,settingsComponent); 
       _settingsXmlTimmer.start(); 
      } 

     } 

     //timmer 
     private function settingsComponent(event:TimerEvent):void { 
      //_settingsXmlTimmer.stop(); 
      //trace ("initial is running: "+_settingsXmlTimmer.running); 
      if (_loadSettingsXml.isxmlread && _settingsXmlTimmer.running) { 
       _settingsXmlTimmer.stop(); 
       //trace ("after is running: "+_settingsXmlTimmer.running); 

       loaderIcon_mc.visible=true; 
       loaderIcon_mc.x=(_loadSettingsXml._myXml.record[0].scrollComponentWidth-loaderIcon_mc.width)/2; 
       loaderIcon_mc.y=(_loadSettingsXml._myXml.record[0].scrollComponentHeight-loaderIcon_mc.height)/2; 



       _txt.width = _loadSettingsXml._myXml.record[0].scrollComponentWidth-40; 
       _txt.height = _loadSettingsXml._myXml.record[0].scrollComponentHeight; 
       _txt.mouseWheelEnabled=true; 
       _txt.multiline = true; 
       _txt.wordWrap = true; 
       _txt.selectable = true; 
       _txt.condenseWhite=true; 
       _txt.border=false; 

       _txt.x=0; 
       _txt.y=0; 



       if (_loadSettingsXml._myXml.record[0].cssStylesPath!='') { 
        _urlCSS=new URLRequest(_loadSettingsXml._myXml.record[0].cssStylesPath); 
        _loaderCSS.addEventListener(Event.COMPLETE, onCSSFileLoaded); 
        _loaderCSS.load(_urlCSS); 
       }    

      } 
     }    


     private function readHTML() :void 
     { 
      if (_loadSettingsXml._myXml.record[0].htmlPath!='') { 
       _urlHTML=new URLRequest(_loadSettingsXml._myXml.record[0].htmlPath); 
       _loaderHTML.addEventListener(Event.COMPLETE, onHTMLFileLoaded); 
       _loaderHTML.load(_urlHTML); 
      } 
     } 


     private function addHTMLandScroll() :void 
     { 
      addChild(_txt); 
      if (_txt.height<_txt.textHeight) { 
       _MyScrollBar.x=_txt.x+_txt.width+20; 
       _MyScrollBar.y=_txt.y+14; 
       _MyScrollBar._height=_txt.height-28; 
       _MyScrollBar._txt=_txt; 
       _MyScrollBar.scrollable_area_mc.height=_txt.height-28; 
       _MyScrollBar.scrollDownButton.y=_txt.height-14; 
       _MyScrollBar.initPublicVars(); 
       addChild(_MyScrollBar); 
      }   
     }  


     private function onCSSFileLoaded(event:Event):void { 
      _sheet.parseCSS(_loaderCSS.data); 
      //_timer_txt.styleSheet = sheet; 
      _txt.styleSheet = _sheet; 
      readHTML(); 
     }  

     private function onHTMLFileLoaded(event:Event):void { 
      _txt.htmlText = _loaderHTML.data; 
      loaderIcon_mc.visible=false; 
      addHTMLandScroll(); 
     }   


    } 

} 

有關如何解決這個問題的任何建議?我有點新在這..

回答

0

那麼從我得到你想__xmlSettingsPath設置從課堂以外的每個實例,儀式?

您可能會考慮不首先初始化變量,或者將其設置爲空字符串,因爲您在使用該變量的其他函數中已經進行了檢查。

private var __xmlSettingsPath:String = ""; 

更改構造函數以接受該變量。

public function scrolledTextComponent (XmlPath:String) 
    { 
     __xmlSettingsPath:String = XmlPath; 

     loaderIcon_mc.visible=true; 
     readSettingsXml();  
    } 

所以基本上必須同時調用組件類稱其爲

new scrolledTextComponent(XmlPathforThisInstance) 

希望有,因爲我還沒有在代碼看着也沒有其他問題上作出上述兩個變化&。


編輯:

要調用不同的情況下,你可以聲明變量分配差異的對象。

VAR FirstInstance:scrolledTextComponent =新scrolledTextComponent(FirstXmlPath)

VAR ThirdInstance:scrolledTextComponent =新scrolledTextComponent(ThirdXmlPath)

VAR FifthInstance:scrolledTextComponent =新scrolledTextComponent(FifthXmlPath)

....等

+0

感謝您的回覆。我理解第一部分,但是我將如何/將在哪裏將第二,第三,第四部分稱爲.. scrolledtextcompnents? – AlxSTi 2010-12-11 16:31:07

+0

看看編輯。 – loxxy 2010-12-12 03:12:39