Flash AS3编码变量针对每个调用进行了不同的初始化

时间:2010-12-10 23:35:13

标签: flash actionscript-3 variables flash-cs5

我的Flash CS5 / AS3项目中有一个内容滚动条,我想在多个地方使用,同时在这些不同的地方加载不同的内容(html)。

以下是滚动文本组件的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();
        }           


    }

}

有关如何解决此问题的任何建议?我对此有点新意。

1 个答案:

答案 0 :(得分:0)

从我得到的你希望__xmlSettingsPath从课外设置每个实例,仪式?

您可以考虑不首先初始化变量,或者将其设置为空字符串,因为您在使用它的其他函数中进行了检查。

private var __xmlSettingsPath:String = "";

更改构造函数以接受变量。

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

        loaderIcon_mc.visible=true;
        readSettingsXml();    
    }

所以基本上你必须做出以上两个改变&amp;在调用组件类时,将其称为

new scrolledTextComponent(XmlPathforThisInstance)

希望没有其他问题,因为我没看好代码。


修改

要调用不同的实例,可以声明变量以分配diff对象。

var FirstInstance:scrolledTextComponent = new scrolledTextComponent(FirstXmlPath)

var ThirdInstance:scrolledTextComponent = new scrolledTextComponent(ThirdXmlPath)

var FifthInstance:scrolledTextComponent = new scrolledTextComponent(FifthXmlPath)

....等等