A Flash Developer Resource Site

Results 1 to 14 of 14

Thread: Passing vars in AS3 without using URL string?

  1. #1
    Junior Member
    Join Date
    Feb 2008
    Posts
    4

    Cool Passing vars in AS3 without using URL string?

    Hello,

    I have one AS3 swf (loader.swf) that needs to load a second AS3 file (main.swf).

    loader.swf has some variables that I would like to pass to main.swf.

    I don't want to append the variables in the URL string, because then main.swf won't be cached properly.

    In AS2 I could use FlashVars, which passes the variables without adding them to the URL string.

    How can I do this in AS3?

  2. #2
    Senior Member
    Join Date
    Jan 2008
    Location
    UK
    Posts
    269
    I don't want to append the variables in the URL string, because then main.swf won't be cached properly.
    Not sure if this would work, but you could try adding a random number to the end of the appended variables, to prevent caching problems. It works for passing variables to php scripts - the random number leads the system to believe it is a different php script than the last one and hence doesn't rely on the cached version.

  3. #3
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    In the javascript in the html you add the variables here:

    'movie', 'mymovie?var1=this is var1',

    In your movie you get the variable using this:
    var myVar:String = loaderInfo.parameters["var1"];
    - The right of the People to create Flash movies shall not be infringed. -

  4. #4
    Junior Member
    Join Date
    Feb 2008
    Posts
    4

    Urgent! Weird undesired caching in AS3 loader due to URL string

    Maybe I am not being clear. I WANT to cache the second (main.swf) file. But I also want to pass it a parameter, not using the query string. The parameter is pretty much random, but when it's in the URL, it causes browsers to NOT cache the main.swf file, since it thinks it's another request.
    Last edited by KeithAlan; 02-13-2008 at 01:03 PM. Reason: clarification

  5. #5
    Senior Member
    Join Date
    Jan 2008
    Location
    UK
    Posts
    269
    Ah - I see. Sorry, I don't know how to do that directly. You could always get the loader to send the variables in a url string to a php script, and have the script save them to a server side text file or database. You could then have the main swf read them in - although it's probably a messy workaround if someone knows of a direct method.

  6. #6
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Load your second movie without params, then call a method in it to initialize from params as arguments. To do this you must cast the loaded movie to its actual class (or MovieClip). You may also have to specify that it should load in the same ApplicationContext.

  7. #7
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    I found this using javascript (2nd example):

    http://www.manuelnegri.com/web_design/flash_tut1.htm
    - The right of the People to create Flash movies shall not be infringed. -

  8. #8
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Keith, if you're still having problems with this, I may be able to throw some code up later tonight. Did my answer help at all, or should I create a proof of concept?

  9. #9
    Senior Member cancerinform's Avatar
    Join Date
    Mar 2002
    Location
    press the picture...
    Posts
    13,448
    I found out you can use Flashvars. This is in your html-javascript (only a part I wrote here):

    'height', '400',
    'src', 'passvarjavascript',
    'FlashVars','var1=hello',
    'quality', 'high',
    'pluginspage', 'http://www.macromedia.com/go/getflashplayer',
    'align', 'middle',

    This is in your movie:
    PHP Code:
    var var1:String=loaderInfo.parameters.var1;
    try
    {
        
    myText.text=var1;
    }
    catch (
    e:Error)
    {
        
    myText.text=String(e+": var1");

    Of course this is only an example. Your movie should stay in cache.
    - The right of the People to create Flash movies shall not be infringed. -

  10. #10
    Junior Member
    Join Date
    Feb 2008
    Posts
    4
    Hi, 5TonsOfFlax,

    I would appreciate it quite a bit if you could show me some AS3 code that loads a swf file with parameters, without passing them in on the query string. I want to ensure that the huge main.swf files are cached and not re-requested every time from our server.

    Keep in mind: I cannot change the main.swf file, I don't control it. I can only pass a variable into it, such as "userId". Thank you for the help!

  11. #11
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    Ah, I was assuming you had some degree of control over the main.swf. If you don't, then my solution is not practical. You could end up setting variables, but you have no way of making sure they are set when the loaded movie initializes. The flashvars stuff is a good start. I will have to do some fiddling around to see if loaded movies inherit parameters from their parents (if they do, that solves your problem). If not, I'm afraid I don't have a good solution for you.

    Edit:
    I have an idea about using URLVariables in your URLRequest and sending as POST if the easy way doesn't work. I've no idea whether that will work either. And I cannot work on this until tomorrow at the earliest.
    Last edited by 5TonsOfFlax; 02-14-2008 at 06:04 PM.

  12. #12
    Junior Member
    Join Date
    Feb 2008
    Posts
    4
    Thank you. Let me know if you were able to get some POST-based code to work. I appreciate it!

  13. #13
    Junior Member
    Join Date
    Feb 2008
    Posts
    24
    How are you loading that main.swf?
    Generally, you can access the loaded swf like a movie clip, that means the container swf can modify all public properties of the loaded file.

  14. #14
    Junior Member
    Join Date
    Feb 2009
    Posts
    1
    has anyone found a solution for this?
    loading an external SWF, passing variables to it like FlashVars ,without query string?

    EDIT:
    sorry for popping up such an old thread...
    Last edited by mobilhood; 02-02-2009 at 05:19 AM.

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