5 Replies Latest reply on Jul 31, 2007 2:01 PM by Newsgroup_User

    Accessing swf parameters in flash 9 AS 3.0

    SamDonaldson
      Hello,

      I'm having difficulty understanding how swf URL params are accessed? For example, say I redirect to: test.swf?blah=blah1&blah2=blah3....

      How does my swf get access to blah, blah2 etc..? What actionscript 3.0 is required to get access to these variables? Note, that I want access to them in my .as files. Any code snippet describing how to do this would be helpful.

      Thanks,

      Sam
        • 1. Re: Accessing swf parameters in flash 9 AS 3.0
          Level 7
          Sam,

          > I'm having difficulty understanding how swf URL params
          > are accessed? For example, say I redirect to:
          > test.swf?blah=blah1&blah2=blah3....

          By appending those variables to the SWF's URL, you're doing the same
          thing, essentially, as declaring them in frame 1 of the main timeline.

          > How does my swf get access to blah, blah2 etc..?

          Just refer to them as you would any normal String variable (they're
          going to be strings, if you pass them in, even if you use numbers of words
          like "true" or "false"). If you declare a variable in frame 1, you could
          access it in frame 2, 10, or 110 (or even on frame 1, afrer you declare it).

          > What actionscript 3.0 is required to get access to these
          > variables?

          If you've passed in blah=blah1, then you would refer to "blah" (without
          quotes) as you would any other variable. You might, for example, check
          blah's value with an if() statement:

          if (blah == "blah1") {
          ...
          }

          In this case, you would use quotes around "blah1" because you're
          checking the String value of the blah variable.

          > Note, that I want access to them in my .as files. Any code
          > snippet describing how to do this would be helpful.

          Code in AS files doesn't really "live" until it's in the SWF anyway. If
          your AS code would be able to reference a variable declared in frame 1 --
          and this would be a good proof of concept for you -- it would also be able
          to reference the query-string passed in variables (same as it would
          FlashVars, which is basically another approach toward the same goal).


          David Stiller
          Adobe Community Expert
          Dev blog, http://www.quip.net/blog/
          "Luck is the residue of good design."


          • 2. Re: Accessing swf parameters in flash 9 AS 3.0
            SamDonaldson Level 1
            This didn't really work. I'm new to this so these might be some silly questions but basically, my app has a bunch of .as files and a .fla file that I created. My .fla file instantiates the main class in one of the .as files. Now, I want to access the URL param in my .as file but the compiler errors out saying I don't have that variable declared if I just make a naked access to the variable as you suggested. There is only 1 frame to my app but still, I can't get access to it by just referencing it. I don't understand how that'd word either. Are you saying I should access it in my fla file and then stuff it in some top level object?
            • 3. Re: Accessing swf parameters in flash 9 AS 3.0
              Level 7
              Sam,

              > This didn't really work.

              ... Yes, and I owe you an apology, Sam. I see that I've cavalierly
              steered you the wrong way, still thinking in terms of AS2. Rather than help
              you, I've only made your headache worse, and I truly regret that.

              > I'm new to this so these might be some silly questions but
              > basically, my app has a bunch of .as files and a .fla file
              > that I created.

              The thing that has me chagrined is that I'm not new to this, and yet I
              gave you an automatic pat answer without remembering a few basic AS3
              differences. The best I can do at this point is to correct my error.

              > Are you saying I should access it in my fla file and then
              > stuff it in some top level object?

              I wasn't intending to suggest that, but in a sense, a top level object
              is what ends up giving you what you're after. As you've discovered, the
              strictness of the AS3 compiler doesn't like references to undeclared
              variables. In any case, variables declared outside a package definition are
              not accessible within the package (p. 302, Moock's Essential ActionScript
              3.0). So ... how are these "extra curricular" variables to be referenced?

              Now that I'm on the right track myself, here's what I've found: all
              DisplayObjects -- this includes the main timeline -- have a loaderInfo
              property, which returns a LoaderInfo instance. According to the docs, "The
              loaderInfo property is defined only for the root display object of a SWF
              file or for a loaded Bitmap (not for a Bitmap that is drawn with
              ActionScript)." So, you'll be referencing the root's loaderInfo property
              and further referencing that object's parameters property, which contains
              (again, quoting from the docs) "An object that contains name-value pairs
              that represent the parameters provided to the loaded SWF file."

              As always, senocular provides a useful, succinct example. In this case,
              it's one of this many AS3 tips here:

              http://www.kirupa.com/forum/showthread.php?p=2110835

              ... look for the "Accessing FlashVars and HTML Parameters" heading on that
              page.


              David Stiller
              Adobe Community Expert
              Dev blog, http://www.quip.net/blog/
              "Luck is the residue of good design."


              • 4. Re: Accessing swf parameters in flash 9 AS 3.0
                SamDonaldson Level 1
                Ok, I'll try that. I just wanted to get some concepts clear here:

                root - this variable points to the root of the current objects display hierarchy. My question is, is this variable only available if the hierarchy has been added to the stage? What happens when you access the root variable in a display container object that is the child of a bunch of other container objects but those container objects have not been added to the stage, leaving the hierarchy, in some sense, detached.

                If my display object hierarchy is added to the stage, does this mean root will always point to the main class object that is attached to the stage at level 0?

                Note that all of this has to do with just 1 frame.

                thanks.
                • 5. Re: Accessing swf parameters in flash 9 AS 3.0
                  Level 7
                  Sam,

                  > root - this variable points to the root of the current objects
                  > display hierarchy. My question is, is this variable only
                  > available if the hierarchy has been added to the stage?

                  Running the following quick code gives the following output:

                  trace(root);

                  var mc:MovieClip = new MovieClip();
                  trace(mc.root);

                  addChild(mc);
                  trace(mc.root);

                  // Outputs ...
                  [object MainTimeline]
                  null
                  [object MainTimeline]

                  That tells me your hunch is correct: that the DisplayObject seems to
                  need to belong to a display list before its root property has a value.

                  > What happens when you access the root variable in a ... container
                  > ... [that has] not been added to the stage, leaving the hierarchy, in
                  > some sense, detached[?]

                  Looks like a null reference ... but I haven't delved *so* deeply into
                  AS3 yet that I have a good, solid answer to this yet. :-/ My guess is that
                  Moock's "Essential ActionScript 3.0" is the reference to really spell this
                  one out.

                  > If my display object hierarchy is added to the stage, does this
                  > mean root will always point to the main class object that is
                  > attached to the stage at level 0?

                  As opposed to _level1, _level2, etc? Good question! Presumably, any
                  external SWF that you load would be added to a display list in the main SWF.
                  I'll definitely have to check into this, but I don't know if I'll remember
                  to come back to this particular thread and write about what I find. Seems
                  like this will take a bit of reading and experimenting, which may take some
                  time.


                  David Stiller
                  Adobe Community Expert
                  Dev blog, http://www.quip.net/blog/
                  "Luck is the residue of good design."