A Flash Developer Resource Site

Page 1 of 3 123 LastLast
Results 1 to 20 of 41

Thread: How I can get Flashvars value from a package as class, not from the timeline?

  1. #1
    Senior Member
    Join Date
    Aug 2001
    Location
    Seattle, WA
    Posts
    180

    How I can get Flashvars value from a package as class, not from the timeline?

    I need to assign dynamically a path to a xml file through Flashvars. How I can make it work?

    I was able to get Flashvar from html page into swf with actionscript code on timeline:

    1. HTML – assigning Flashvars with swfObject:

    Code:
    <script type="text/javascript">
    var flashvars = {};
    flashvars.xmlfile = "xml/data.xml";
    var params = {};
    var attributes = {};
    attributes.id = "IdScroller";
    
    swfobject.embedSWF("scroller.swf", "flashDiv", "975px", "600px", "9,0,0,0", "expressInstall.swf", flashvars, params, attributes);
    </script>
    2. Actionscript code on timeline:
    Actionscript Code:
    var XMLpath:String;

    //get flashvar value
    if(root.loaderInfo.parameters["xmlfile"] != null){
        XMLpath = root.loaderInfo.parameters["xmlfile"];
    }

    var loader:URLLoader=new URLLoader();
    loader.addEventListener(Event.COMPLETE,completeHandler);

    // assign that value to URLRequest
    var request:URLRequest = new URLRequest(XMLpath);
    try
    {
        loader.load(request);
    }
    catch(error:Error)
    {
        trace('Error on loading the image.');
    }

    It works great, but I really need to make it work within a package class. I have trouble to make it.
    I am using EventDispatcher to load XML where I define the location path with this method. Below is the code fom this class:

    Actionscript Code:
    package com {
       
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.display.Sprite;

    public static const CONFIG_LOADED:String = "configLoaded";
    public static const DATA_XML_URL:String = "xml/data.xml";
    private var configXMLLoader:XMLLoader;
    private var xml:XML;
    private static var _configManager:ConfigManager;

       
    public class ConfigManager extends EventDispatcher {


    public function load():void {
               
        // Create a new XMLLoader object   
        configXMLLoader = new XMLLoader(CONFIG_XML_URL);
               
        // Add an event listener to be dispatched when xml is parsed.
        configXMLLoader.addEventListener(XMLLoader.XML_LOADED, onConfigXMLLoaded);
               
        // Start loading the file
        configXMLLoader.load();
               
    }

    //This method is called when the configuration file is loaded.       
    private function onConfigXMLLoaded(evt:Event):void {
               
    // Read the setting values from the configuration file
    readConfig(configXMLLoader.getXML());
    dispatchEvent(new Event(ConfigManager.CONFIG_LOADED));

    }
                   
    //Returns the configuration manager.
           
    public static function getInstance():ConfigManager {
               
        if (_configManager == null) {
        _configManager = new ConfigManager(new SingletonEnforcer());
        }
               
        return _configManager;
               
        }  
    }
       
    }

    I cannot make it work at all. It will be great to find the proper way to do it. Please help! Thank you in advance.
    Last edited by vladc77; 05-27-2010 at 04:47 PM.
    Best regards
    Vlad,

  2. #2
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    you can only get the flashVars from the timeline or document root file........get them and then pass them to the class where you want to use them.
    ~calmchess~

  3. #3
    Senior Member
    Join Date
    Aug 2001
    Location
    Seattle, WA
    Posts
    180
    Quote Originally Posted by calmchess View Post
    you can only get the flashVars from the timeline or document root file........get them and then pass them to the class where you want to use them.
    Any ideas on how I can pass it to the event dispatcher I mentionded above. Thank you again.
    Best regards
    Vlad,

  4. #4
    Senior Member
    Join Date
    Aug 2001
    Location
    Seattle, WA
    Posts
    180
    I also wanted to know how I can pass flashvars to a child swf. I am loading an external swf into main swf. The loaded swf has to be able to read flashvars. Any ideas???
    Thanks.
    Best regards
    Vlad,

  5. #5
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    you have to create a custom event dispacther which can take an array as a parameter......i use an external class witch extends event dispatcher and handles assiging the variable to the dispatch.....i can send you one if you like.
    ~calmchess~

  6. #6
    Senior Member
    Join Date
    Aug 2001
    Location
    Seattle, WA
    Posts
    180
    It will be great. I am using this technique forst time and have lots of milestones. Thank you again.

    By the chance, I am wondering if you know how I can pass flashvars to a child swf. I am loading an external swf into main swf. The loaded swf has to be able to read flashvars. Any ideas???
    Thanks.
    Best regards
    Vlad,

  7. #7
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    here yo go get this installed in the same folder as the class you want to intiate the dispatch from and then i'll tell you how to call it.
    Attached Files Attached Files
    ~calmchess~

  8. #8
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    here is the dispatch with two strings being sent as a test you can use variables which are holding data instead of strings also.
    PHP Code:
    dispatchEvent(new CustomEvent0("CustomEvent",false,false,"data0_tosend","data1_tosend")); 
    ~calmchess~

  9. #9
    Senior Member
    Join Date
    Aug 2001
    Location
    Seattle, WA
    Posts
    180
    Thank you for sending me as code. I am highly appreciate it.

    I did install it in the class' directory. I have confiManager class which defines path to xml at this moment. This class public class ConfigManager extends EventDispatcher. Your class is in the same directory now. Please tell me what next I have to do and test it. Thanks.
    Best regards
    Vlad,

  10. #10
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    put some traces inside the file i sent and dispatch the event to make sure it works.......you do know how to specify the package location from within the file i sent you right?
    ~calmchess~

  11. #11
    Senior Member
    Join Date
    Aug 2001
    Location
    Seattle, WA
    Posts
    180
    I am getting the error:
    super(type, bubbles, cancelable);
    I added this:
    Actionscript Code:
    dispatchEvent(new CustomEvent0("CustomEvent",false,false,"data0_tosend","data1_tosend"));

    to ConfigManager.as which I described above in the function below:

    Actionscript Code:
    private function onConfigXMLLoaded(evt:Event):void {
               
                // Read the setting values from the configuration file
                readConfig(configXMLLoader.getXML());
                dispatchEvent(new CustomEvent0("CustomEvent",false,false,"data0_tosend","data1_tosend"));
                dispatchEvent(new Event(ConfigManager.CONFIG_LOADED));

            }

    Unfortunatelly, I am getting error, no trace statements. Did I do something wrong? I also cannot seen how it will read flashvars I am getting on the timeline. Thanks again.
    Best regards
    Vlad,

  12. #12
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    we need to clarify this are you wanting to get the flashVars and then use them in the xml loader or do you want to get the variables and dispatch them to another class?
    ~calmchess~

  13. #13
    Senior Member
    Join Date
    Aug 2001
    Location
    Seattle, WA
    Posts
    180
    I need to pass XML path via FlashVars to the class below.
    I need to assign Flashvars string which I can get in the timeline to "DATA_XML_URL" I have in this class.

    Actionscript Code:
    package com {
       
    import flash.events.Event;
    import flash.events.EventDispatcher;
    import flash.display.Sprite;

    public static const CONFIG_LOADED:String = "configLoaded";
    public static const DATA_XML_URL:String = "xml/data.xml";
    private var configXMLLoader:XMLLoader;
    private var xml:XML;
    private static var _configManager:ConfigManager;

       
    public class ConfigManager extends EventDispatcher {


    public function load():void {
               
        // Create a new XMLLoader object   
        configXMLLoader = new XMLLoader(CONFIG_XML_URL);
               
        // Add an event listener to be dispatched when xml is parsed.
        configXMLLoader.addEventListener(XMLLoader.XML_LOADED, onConfigXMLLoaded);
               
        // Start loading the file
        configXMLLoader.load();
               
    }

    //This method is called when the configuration file is loaded.       
    private function onConfigXMLLoaded(evt:Event):void {
               
    // Read the setting values from the configuration file
    readConfig(configXMLLoader.getXML());
    dispatchEvent(new Event(ConfigManager.CONFIG_LOADED));

    }
                   
    //Returns the configuration manager.
           
    public static function getInstance():ConfigManager {
               
        if (_configManager == null) {
        _configManager = new ConfigManager(new SingletonEnforcer());
        }
               
        return _configManager;
               
        }  
    }
       
    }

    this is where I define static path to the data.xml. I need to modify it in a way I can pass it with FlashVars. Basically, I need to path a string of the actual data.xml file location.
    Last edited by vladc77; 05-27-2010 at 06:56 PM.
    Best regards
    Vlad,

  14. #14
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    here yo go get this installed in the same folder as the class you want to intiate the dispatch from and then i'll tell you how to call it.
    ~calmchess~

  15. #15
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    oops sorry get the flashVar from the document root and pass it to the class through the
    PHP Code:
    var:Classname =new Classname(root.loaderInfo.parameters.flashVarName); 
    ~calmchess~

  16. #16
    Senior Member
    Join Date
    Aug 2001
    Location
    Seattle, WA
    Posts
    180
    Yes, I did it. The class you provided is placed at the same directory where dispatcher class is located. What next I should do? Thanks.
    Best regards
    Vlad,

  17. #17
    Senior Member
    Join Date
    Aug 2001
    Location
    Seattle, WA
    Posts
    180
    I placed it in the frame of the fla I use.
    Actionscript Code:
    var:Classname = new Classname(root.loaderInfo.parameters["xmlfile"]);

    I am getting error:
    1084: Syntax error: expecting identifier before colon.
    I am not sure what else I need to do. I need to assign Flashvars string which I can get in the timeline to "DATA_XML_URL" I have in this 'public class ConfigManager extends EventDispatcher'. I included this code above. Please let me know how it is possible to achieve. Thanks.
    Last edited by vladc77; 05-27-2010 at 06:59 PM.
    Best regards
    Vlad,

  18. #18
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    thats just an example it doesn't do anything do you know how to call a class from your document root? do you have a document root attached to your fla?
    ~calmchess~

  19. #19
    Senior Member calmchess's Avatar
    Join Date
    Sep 2006
    Location
    Earth
    Posts
    2,588
    is the class you posted above the only class you have? if it is then you can get your flashVar directly out of it.
    ~calmchess~

  20. #20
    Senior Member
    Join Date
    Aug 2001
    Location
    Seattle, WA
    Posts
    180
    Not, I have other classes. This paricular 'public class ConfigManager extends EventDispatcher' has a reference to the statc path to data.xml which I need to modify to dynamic.
    Best regards
    Vlad,

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center