Passing flashvars-style parameters to a loaded SWF
Asked Answered
L

4

5

I have a Flex 3 app (player v9) which loads a Flash SWF (AS3, also player v9) and needs to dynamically pass it a collection of parameters which are known at run-time. These are parameters that are normally passed via the flashvars element in an HTML page. The embedded movie accesses these parameters via the loaderInfo.parameters object.

I've tried using SWFLoader and Loader classes with no success in param-passing.

Relevant details:

  • It's a local program, and cannot rely on query string parameters.
  • I've mucked with setting loaderInfo.parameters["foo"] = "123" from the embedding code, but the parameter never seems to wind up in the embedded movie.
  • I cannot place extra parameter-passing machinery in the embedded movie(s), as they are created by third parties.
Lermontov answered 2/1, 2009 at 19:52 Comment(0)
D
9

Passing this params in URL won't help, because they're taken using javascript code in the html-wrapper. The 'flashVars' params are taken using the Application.application.parameters, so, you have to set these params manually in your case.

If you are using SWFLoader to load another app, you should create the object, that will represent the application loaded and apply all you need:

<mx:Script>
    <![CDATA[
        import mx.managers.SystemManager;
        import mx.controls.Alert;
        import mx.events.FlexEvent;

        private var loadedApp:Application;

        private function onLoadComplete(event:Event):void {
            var smAppLoaded:SystemManager = SystemManager(event.target.content);
            smAppLoaded.addEventListener(FlexEvent.APPLICATION_COMPLETE, onLoadedAppComplete);
        }

        private function onLoadedAppComplete(event:FlexEvent):void {
            try {
                loadedApp = Application(event.target.application);
                if(!loadedApp) throw new Error();

                loadedApp.parameters["param1"] = "value1";
            } catch (e:Error) {
                Alert.show("Failed to get application loaded.", "Error", Alert.OK); 
            }
        }

        private function onLoadError():void {
            Alert.show("Failed to load an application.", "Error", Alert.OK);
        }

    ]]>
</mx:Script>

<mx:SWFLoader 
    width="100%" height="100%"
    source="./AppToLoad.swf" 
    complete="onLoadComplete(event)" 
    ioError="onLoadError()" securityError="onLoadError()" />

Decontaminate answered 15/1, 2009 at 17:40 Comment(1)
This is a good general idea. If you want to be more strict about it, you can define an interface for the loaded SWF, and use that to pass strictly-typed parameters into it.Eckert
F
1

The reason is simple. I've discovered this today.

In the component loaded via SWFloader has parentApplication or Aplication.application set to the top level application (this witch loads component via SWFLoader). And the loaded component can see flashvars set to the top level application. This is probably the cause that setting parameters in SWFLoader does not have any impact.

I've set proper flashvars on my toplevel application and they are also seen in the loaded one :-).

Fernferna answered 24/11, 2010 at 12:37 Comment(0)
P
0

When embedding a SWF on a web page you can pass flashvars as parameters on the URL to the SWF, perhaps the same could work in your case? If the SWF is located at file:///some/path/to/a.swf try using file:///some/path/to/a.swf?hello=world&foo=bar. It might work.

Pruinose answered 3/1, 2009 at 10:25 Comment(1)
I'm afraid that doesn't work. It might work if the main SWF were in the browser, but this is running right out of Flash Player.Lermontov
H
0

Would have saved myself a lot of time today if I had found this answer first: AS3 Pass FlashVars to loaded swf.

Essentially: since Flash Player 10.2 it has been possible to pass flashvars along by setting them as parameters on the LoaderContext.

Hollyhock answered 13/6, 2015 at 1:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.