开发者

Flash AS3 coding variables initialized differently for every call

开发者 https://www.devze.com 2023-01-29 19:22 出处:网络
I have a content scroller in my Flash CS5/AS3 project that I want to use in multiple places, while loading different content (html) at each one of those different places.

I have a content scroller in my Flash CS5/AS3 project that I want to use in multiple places, while loading different content (html) at each one of those different places.

Below is the AS3 code for the scrolled text component. The 'compnentsettings.xml' is where I define what .html file to call.

Right now I only have the scroller working in one spot. I can clone it, but of course, it will load the same data.

I believe I have to modify the settingsComponent function from scrolledTextComponent.as (below) to load different compnentSettings.xml files

I will need the variable __xmlSettingsPath initialized different for every call and I have no idea how to do this. Please help!

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;
               开发者_StackOverflow _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();
        }           


    }

}

Any advice on how to go about this? I'm kinda new at this..


Well from what I get you wish __xmlSettingsPath to be set from outside the class every Instance, rite??

You may consider not initializing the variable first, or rather set it to an empty string as you have the checks in place at other functions where it is used.

private var __xmlSettingsPath:String = "";

Change the constructor to accept the variable.

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

        loaderIcon_mc.visible=true;
        readSettingsXml();    
    }

So basically you have to make the above two changes & while calling the component class call it as

new scrolledTextComponent(XmlPathforThisInstance)

Hope there are no other problems as I have not looked well at the code.


Edit:

To call different instances, you may declare variables to assign the diff objects.

var FirstInstance:scrolledTextComponent = new scrolledTextComponent(FirstXmlPath)

var ThirdInstance:scrolledTextComponent = new scrolledTextComponent(ThirdXmlPath)

var FifthInstance:scrolledTextComponent = new scrolledTextComponent(FifthXmlPath)

....so on

0

精彩评论

暂无评论...
验证码 换一张
取 消