28 Replies Latest reply on Sep 22, 2007 10:16 PM by miquael

    FlashVar is AS 3.0 not working

    miquael Level 1
      I am having a problem getting using FlashVars to work with CS3 and AS 3.0.

      I found a supposed "answer" to this problem here in the forum, but it did not make sense to me.

      I have a Flash file that I mastered in AS 2.0 that used FlashVars successfuly. I just rewrote to AS 3.0 with CS3, and cannot get FlashVars to work.

      Questions:

      1) When using an external package class, how do you pass a FlashVar into the class? Is it really the same as passing a var onto the main timeline?

      2) In any case, I also tried placing all my AS 3.0 on the main timeline (stripped of package class form), and still don't get success with FlashVar.

      Can someone post a concrete example of getting FlashVars to work with AS 3.0?

      Thanks.
        • 1. Re: FlashVar is AS 3.0 not working
          SymTsb Level 2
          you are using FlashVars to load data from the HTML page? Store the FlashVars content as a String when the swf starts and send that string over to your class when you instantiate it.
          • 2. FlashVar is AS 3.0 not working
            miquael Level 1
            Okay, I have made an attempt at this, but it is still not working. Here is what I have:

            "dataPath" is the var being passed into the SWF and into the class from HTML. "Nav3DXML.as" is the package class.

            Here is what I have inside the HTML file (showing just the relevant HTML):

            ---------------------------------------------------------
            <object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase=" http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" width="500" height="350" id="MusicCloud" align="middle">
            <param name="allowScriptAccess" value="sameDomain" />
            <param name="movie" value="MusicCloud.swf" />
            <param name="flashvars" value="dataPath=data/sample_u2.xml" />
            <param name="quality" value="high" />
            <param name="bgcolor" value="#000000" />
            <embed src="MusicCloud.swf" flashvars="dataPath=data/sample_u2.xml" quality="high" bgcolor="#000000" width="500" height="350" name="MusicCloud" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage=" http://www.macromedia.com/go/getflashplayer" />
            </object>
            ---------------------------------------------------------

            Here is what I have on the main timeline of the FLA:

            ---------------------------------------------------------
            import Nav3DXML;

            var dataPath:String;

            if (dataPath == "" || dataPath == null) {
            dataPath = "no dataPath provided";
            }

            tx.text = dataPath; // shows value in text field

            Nav3DXML(dataPath);
            ---------------------------------------------------------

            With this, I get "no dataPath provided", which tells me that I'm not even getting the dataPath value into the SWF (let alone passing it on to the class).

            So what is the problem here?

            Assuming that the first problem can be resolved, I have attempted to place "dataPath" in the right places to be received by the Nav3DXML class:

            ---------------------------------------------------------
            package {
            import flash.display.*;
            import flash.events.*;
            import flash.net.*;
            import flash.utils.*;;

            public class Nav3DXML extends Sprite {
            private var node:Node;
            private var nodes:Array;
            private var numNodes:int;
            private var fl:Number = 250;
            private var vpX:Number = stage.stageWidth / 2;
            private var vpY:Number = stage.stageHeight / 2;
            // XML //
            public var dataPath:String; // < - - - HERE
            public var XMLdoc:XML;
            public var URLlocation:String;
            private var loader:URLLoader;

            // constructor
            public function Nav3DXML(dataPath:String) { // < - - - HERE
            init(dataPath); // < - - - HERE
            }

            // main function / load XML
            public function init(dataPath:String):void { // < - - - HERE

            var urlRequest:URLRequest = new URLRequest(dataPath); // < - - - HERE
            loader = new URLLoader();
            loader.dataFormat = URLLoaderDataFormat.TEXT;
            loader.addEventListener( Event.COMPLETE, loadXMLComplete );
            loader.load(urlRequest);

            }
            // ... the class function go on to handle the results of the XML ....
            ---------------------------------------------------------

            In any case, I get an error trying to pass dataPath into the class:

            TypeError: Error #1034: Type Coercion failed: cannot convert "no dataPath provided" to Nav3DXML.

            Any insights?






            • 3. Re: FlashVar is AS 3.0 not working
              SymTsb Level 2
              the proper way to handle the old AS2 FlashVars is done using the Loader display objects loaderInfo.parameters property.

              LoaderInfo objects are available for anything on the stage so accessing your FlashVars would be done something like this I'd believe....

              var myVars:Object = MovieClip(root).loaderInfo.parameters;

              After you've got it in the myVars object, you should be able to edit it, pull it apart etc. just like you did in AS2. FlashVars from HTML do still get sent to the main timeline of a swf but you access them differently than before.
              • 4. Re: FlashVar is AS 3.0 not working
                kglad Adobe Community Professional & MVP
                you also need to add your flashvars to AC_FL_RunContent
                • 5. Re: FlashVar is AS 3.0 not working
                  miquael Level 1
                  Okay, I've had success using LoaderInfo.parameters property, and including the FlashVars within the AC_FL_RunContent ... Thanks so much for this info!

                  I am able to display (or trace) the FlashVar value within the SWF, yet am still not able to pass the value into the class (for further processing).

                  Here is what I now have:

                  - - - - - - - - - - - - - - - - - - - - -
                  import Nav3DXML;
                  var myVars:Object = MovieClip(root).loaderInfo.parameters;
                  tx.text = myVars.dataPath; // shows value in text field
                  Nav3DXML(myVars.dataPath);
                  - - - - - - - - - - - - - - - - - - - - -

                  Yet, I get this error message:

                  TypeError: Error #1034: Type Coercion failed: cannot convert "data/sample_u2.xml" to Nav3DXML.

                  ("data/sample_u2.xml" is the value from the dataPath var in the HTML).

                  I have not updated anything within the class:

                  - - - - - - - - - - - - - - - - - - - - -
                  public function Nav3DXML(dataPath:String) {
                  init(dataPath);
                  }
                  - - - - - - - - - - - - - - - - - - - - -

                  Yet I also tried changing the type to receive an Object instead of a String:

                  - - - - - - - - - - - - - - - - - - - - -
                  Nav3DXML(myVars);
                  - - - - - - - - - - - - - - - - - - - - -
                  public function Nav3DXML(myVars:Object) {
                  init(myVars.dataPath);
                  }
                  - - - - - - - - - - - - - - - - - - - - -

                  And get a similar error:

                  TypeError: Error #1034: Type Coercion failed: cannot convert Object@c812841 to Nav3DXML.

                  Am I not using Type correctly? Any more insights on how to pass a value into the class???
                  • 6. Re: FlashVar is AS 3.0 not working
                    kglad Adobe Community Professional & MVP
                    try:

                    • 7. Re: FlashVar is AS 3.0 not working
                      miquael Level 1
                      Okay, that helps. But now getting this error message:

                      -------------------------------------------------------------
                      ]TypeError: Error #1009: Cannot access a property or method of a null object reference.
                      at Nav3DXML$iinit()
                      -------------------------------------------------------------

                      The error message suggests the property is being passed as null around the class init(). Here is the current class code (relevant lines):

                      -------------------------------------------------------------
                      public function Nav3DXML(dataPath:String) {
                      trace (dataPath)
                      init(dataPath);
                      }

                      public function init(dataPath:String):void {
                      trace (dataPath)
                      var urlRequest:URLRequest = new URLRequest(dataPath);
                      loader = new URLLoader();
                      loader.dataFormat = URLLoaderDataFormat.TEXT;
                      loader.addEventListener( Event.COMPLETE, loadXMLComplete );
                      loader.load(urlRequest);
                      }
                      -------------------------------------------------------------

                      None of the trace commands here are activating.

                      I have also tried passing the entire object to the constructor function (with the same results):

                      -------------------------------------------------------------
                      public function Nav3DXML(myVars:Object) {
                      trace (myVars.dataPath)
                      init(myVars.dataPath);
                      }

                      public function init(myVars:Object):void {
                      trace (myVars.dataPath)
                      var urlRequest:URLRequest = new URLRequest(myVars.dataPath);
                      loader = new URLLoader();
                      loader.dataFormat = URLLoaderDataFormat.TEXT;
                      loader.addEventListener( Event.COMPLETE, loadXMLComplete );
                      loader.load(urlRequest);
                      }
                      -------------------------------------------------------------

                      It is so close! Any more insights? I must get this (ASAP)!


                      -------------------------------------------------------------


                      • 8. Re: FlashVar is AS 3.0 not working
                        kglad Adobe Community Professional & MVP
                        you probably have white space at the end of your dataPath. use a trace() function to confirm and a trim function to remove the white space.
                        • 9. FlashVar is AS 3.0 not working
                          SymTsb Level 2
                          The problem I believe occurs in logic in the constructor... You have a variable declared called dataPath in your class file. The constructor takes an arguement called dataPath and then you call init with dataPath as the value of your variable. However, Flash does not send dataPath from the instantiation to your script. Instead it takes the value of the declared dataPath which has not been initialized. Try the code I provided with the changes and see if you get further into the script before the next error. I also noticed you have a variable declared with type Node which I believe is supposed to be XMLNode. You might want to change that as well.
                          • 10. Re: FlashVar is AS 3.0 not working
                            miquael Level 1
                            nope, no white spaces. and did a trace before sending into the class:

                            trace ("***" + myVars.dataPath + "***"); // returns ***data/sample_u2.xml***
                            • 11. Re: FlashVar is AS 3.0 not working
                              miquael Level 1
                              I carefully replaced this code, and it is still not working!!! I get the same error message as before:

                              ----------------------------
                              TypeError: Error #1009: Cannot access a property or method of a null object reference.
                              at Nav3DXML$iinit()
                              ----------------------------

                              this is on the maintimeline:

                              ----------------------------
                              var myNav3D:Nav3DXML = new Nav3DXML( this, myVars.dataPath );
                              ----------------------------

                              And here is my class file. (By the way, the node:Node is actually a reference to another class (which is being used by this class in a part I had not included here, so should not be relevant):

                              package {
                              import flash.display.*;
                              import flash.events.*;
                              import flash.net.*;
                              import flash.utils.*;
                              import flash.filters.BlurFilter;
                              import Node;

                              public class Nav3DXML extends Sprite {
                              private var node:Node;
                              private var nodes:Array;
                              private var numNodes:int;
                              private var fl:Number = 250;
                              private var vpX:Number = stage.stageWidth / 2;
                              private var vpY:Number = stage.stageHeight / 2;
                              // XML //
                              public var dataPath:String;
                              public var XMLdoc:XML;
                              public var URLlocation:String;
                              private var loader:URLLoader;
                              private var container:MovieClip;

                              // constructor
                              public function Nav3DXML(target:MovieClip, dP:String) {
                              container = target;
                              dataPath = dP;
                              trace (dataPath)
                              init(dataPath);
                              }
                              // main function / load XML
                              private function init(dP:String):void {
                              trace (dataPath);
                              var urlRequest:URLRequest = new URLRequest(dataPath);
                              loader = new URLLoader();
                              loader.dataFormat = URLLoaderDataFormat.TEXT;
                              loader.addEventListener( Event.COMPLETE, loadXMLComplete );
                              loader.load(urlRequest);
                              }
                              }
                              }

                              Even with this, none of the traces don't activate still. What is going on here?!!!

                              What is the purpose of the container?

                              And now, I event made an attempt to put the dataPath value in directly (just be be sure the application still works), and I get the SAME ERROR MESSAGE!

                              Main timeline:

                              var myNav3D:Nav3DXML = new Nav3DXML();

                              Changes this inside class:

                              // constructor
                              public function Nav3DXML() { //
                              init();
                              }
                              // main function / load XML
                              private function init():void {
                              var urlRequest:URLRequest = new URLRequest("data/sample_u2.xml"); <--- DIRECT VALUE
                              loader = new URLLoader();
                              loader.dataFormat = URLLoaderDataFormat.TEXT;
                              loader.addEventListener( Event.COMPLETE, loadXMLComplete );
                              loader.load(urlRequest);
                              }


                              Again, error message is:

                              TypeError: Error #1009: Cannot access a property or method of a null object reference.
                              at Nav3DXML$iinit()

                              WFT!?





                              • 12. Re: FlashVar is AS 3.0 not working
                                kglad Adobe Community Professional & MVP
                                you have no access to the stage properties.
                                • 13. Re: FlashVar is AS 3.0 not working
                                  miquael Level 1
                                  What do you mean? I thought we took care of this already (access to stage properties), with this on the main timeline:

                                  var myVars:Object = MovieClip(root).loaderInfo.parameters;

                                  That resolved getting dataPath into the SWF. And it appears that dataPath may be getting into the class, yet this error is boggling my mind!

                                  TypeError: Error #1009: Cannot access a property or method of a null object reference.
                                  at Nav3DXML$iinit()

                                  So it appears the problem is with the init(), but I am not sure exactly what. I really need to sort this out, and I'm not sure what to do.

                                  I've actually taken SymTsb's code and directly copied it over my existing class. I still get the same error. Here is everything I have:



                                  • 14. Re: FlashVar is AS 3.0 not working
                                    miquael Level 1
                                    Thanks for all of your help! I really hope that there is a solution ...
                                    • 15. Re: FlashVar is AS 3.0 not working
                                      kglad Adobe Community Professional & MVP
                                      the problem is

                                      private var vpX:Number = stage.stageWidth / 2;
                                      private var vpY:Number = stage.stageHeight / 2;

                                      your class has no access to the stage class.
                                      • 16. Re: FlashVar is AS 3.0 not working
                                        miquael Level 1
                                        Ah, you are right. I replaced those two lines with direct values, and it resolved that specific error:

                                        private var vpX:Number = 400;
                                        private var vpY:Number = 250;

                                        I'm not sure why this fixed it (!?). Yet now the traces work within the class, and it appears that the data is loaded as passed onto the next function. So the dataPath value is successfully passed and received into the class!!!

                                        But now a new version of the error emerges in the next function:

                                        TypeError: Error #1009: Cannot access a property or method of a null object reference.
                                        at Nav3DXML/::loadXMLComplete()
                                        at flash.events::EventDispatcher/flash.events:EventDispatcher::dispatchEventFunction()
                                        at flash.events::EventDispatcher/dispatchEvent()
                                        at flash.net::URLLoader/flash.net:URLLoader::onComplete()

                                        What's up with this "null object reference"?

                                        I found this within the next references function (loadXMLComplete), and commented them out to get rid of the error:

                                        //stage.align = StageAlign.TOP_LEFT;
                                        //stage.scaleMode = StageScaleMode.NO_SCALE;

                                        Now I get no errors, it appears that the data is loading, but I am getting no visualization of the data. Perhaps it is not aligned to the stage?

                                        How do I account for the class not having access to the stage?

                                        Here is the current class code, including the next called function:


                                        • 17. Re: FlashVar is AS 3.0 not working
                                          SymTsb Level 2
                                          stage is a property of all the DisplayObjectContainer classes i.e. MovieClip, Loader etc. stage should only be access through these classes. To be able to call it from your own class, you need to create a Stage object and reference its stage properties. When I tested your code to get you moving in the right direction, I commented out these variables as well as the event listener for your loader that you had inside the init method and it did work for me without any errors. Try that and see if at the very least, you are able to get your init() method to fire without error.
                                          • 18. Re: FlashVar is AS 3.0 not working
                                            miquael Level 1
                                            Yes, I have updated the function. Now I get no errors, and can see the XML has loaded (with trace). I attempted to get to stage by referencing the "container" element that was introduced. But I am still not getting a visual.

                                            • 19. Re: FlashVar is AS 3.0 not working
                                              miquael Level 1
                                              My sense is that perhaps the Node class is not being integrated properly. My mistake, the Node class is not a separate .as file (so import Node can be taken out of the class), but is a graphic MC in the main FLA that has a Linkage class as "Node" and a base class of "flash.display.MovieClip". Again, this had all worked before on the main timeline.


                                              • 20. Re: FlashVar is AS 3.0 not working
                                                miquael Level 1
                                                So, obviously, I'm still getting a sense for AS 3.0 and the class structure dynamics.

                                                I can see that both stage alignment and displaying the Node MC will require an object that represents the stage of the DisplayObjectContainer. Not yet exactly sure how to go about that.
                                                • 21. Re: FlashVar is AS 3.0 not working
                                                  SymTsb Level 2
                                                  This is where that container variable will help....

                                                  try calling on node and adding it to the display list....

                                                  var myNode:Node = new container.Node();
                                                  • 22. FlashVar is AS 3.0 not working
                                                    miquael Level 1
                                                    Okay, I updated this, and then got a new error:




                                                    • 23. Re: FlashVar is AS 3.0 not working
                                                      miquael Level 1
                                                      Well, unless anyone has any more ideas ... I guess the short term solution would be to place all the code onto the main timeline (and not within the class).
                                                      • 24. Re: FlashVar is AS 3.0 not working
                                                        scrfix
                                                        Michael,

                                                        I have skimmed through everything here. Instead of using FlashVars, have you attempted to use JavaScripting to pass the values??

                                                        Just an idea. If one way doesn't work, attempt the other.

                                                        Wayne
                                                        • 25. Re: FlashVar is AS 3.0 not working
                                                          miquael Level 1
                                                          Well, first off: the FlashVars aspect of this is working. And second, Flashvars is actually handled (at least partially) with JavaScript within the HTML.

                                                          The problem now is with plotting the graphic nodes to the stage.
                                                          • 26. Re: FlashVar is AS 3.0 not working
                                                            miquael Level 1
                                                            I've identified the problem within this block of code. Here, we are creating a new Node object (which is a graphic object in the Library with a corresponding class), and then assigning various propertied to it before placing on the stage with addChild().

                                                            For some reason (and I am new to working with class packages in AS 3.0), the class does not have direct access to the stage of the SWF. I would love to know the reasoning behind this ... ! This is why I was recommended to add the container MC in order to have the dataPath value passed from the stage to the class (I still do not understand how).

                                                            So now, in order to addChild(node), we need to first somehow associate the node to the container. The attempt to use:

                                                            var node:Node = new container.Node();

                                                            results in this error:

                                                            TypeError: Error #1007: Instantiation attempted on a non-constructor.

                                                            So how to attach the Node to the stage???


                                                            • 27. Re: FlashVar is AS 3.0 not working
                                                              scrfix Level 1
                                                              I am just attempting to come up with new ideas for you. I cannot physically help because you are far more advanced than myself at writing ActionScript.

                                                              However, let me ask you this. Are you writing a new application or are you converting an actionscript 2.0 to a AS3?

                                                              If you are doing a conversion I saw a convertor on http://board.flashkit.com/board that is supposed to do everything for you.

                                                              Wayne
                                                              • 28. Re: FlashVar is AS 3.0 not working
                                                                miquael Level 1
                                                                Actually, in this case, I am not converting directly from AS 2 to AS 3. Although the previous application is in AS 2, I simply found a premade AS 3 3D engine, and extended it for our purposes (so the code is actually quite different).

                                                                But having a AS2 to AS3 converter is a great idea for perhaps expediting other application we need to move over to AS 3! I was not able to locate the one you found on FlashKit, but I did fine this (which I'll look into more): http://www.5etdemi.com/blog/archives/2006/11/as2-to-as3-preprocessor-converter-first-draft /